-
-
Save gorenje/883054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import <Foundation/CPObject.j> | |
@import "UserSessionController.j" | |
@implementation AppController : CPObject | |
{ | |
CPWindow mainWindow; //this "outlet" is connected automatically by the Cib | |
CPMenu mainMenu; | |
CPView contentView; | |
CPToolbar toolbar; | |
CPToolbarItem toolItemUserName; | |
CPSearchField searchField; | |
CPTextField labelUserName; | |
CPString test; | |
UserSessionController i_userSessionController; | |
} | |
- (void)applicationDidFinishLaunching:(CPNotification)aNotification | |
{ | |
} | |
- (void)awakeFromCib | |
{ | |
[CPMenu setMenuBarVisible:NO]; | |
toolbar = [[CPToolbar alloc] initWithIdentifier:"AppToolbar"]; | |
[toolbar setDelegate:self]; | |
[mainWindow setToolbar:toolbar]; | |
[toolbar setVisible:NO]; | |
[toolbar setVisible:NO]; | |
[mainWindow setFullBridge:YES]; | |
[labelUserName setStringValue:""]; | |
[toolbar toolbarItemDidChange:toolItemUserName]; | |
//check auth | |
i_userSessionController = [[UserSessionController alloc] init]; | |
[i_userSessionController checkAuth:self]; | |
[[CPNotificationCenter defaultCenter ] | |
addObserver:self | |
selector:@selector(userNotAuthenticated:) | |
name:@"userNotAuthenticated"]; | |
[[CPNotificationCenter defaultCenter ] | |
addObserver:self | |
selector:@selector(userAuthenticated:) | |
name:@"userAuthenticated"]; | |
} | |
- (void)userNotAuthenticated:(CPNotification)aNotification | |
{ | |
console.debug("User is not authenticated"); | |
[labelUserName setStringValue:""]; | |
[toolbar toolbarItemDidChange:toolItemUserName]; | |
[CPMenu setMenuBarVisible:NO]; | |
[toolbar setVisible:NO]; | |
[[i_userSessionController loginView] setFrame:[contentView bounds]]; | |
[contentView addSubview:[i_userSessionController loginView]]; | |
} | |
- (void)userAuthenticated:(CPNotification)aNotification | |
{ | |
var userSessionContrller = [aNotification object]; | |
console.debug("User is authenticated " + [userSessionController userName]); | |
[labelUserName setStringValue:[userSessionController userName] + @"("+[userSessionController firstName] + @" " + [userSessionController lastName]+ ")"]; | |
[toolbar toolbarItemDidChange:toolItemUserName]; | |
[[i_userSessionController loginView] removeFromSuperview]; | |
[CPMenu setMenuBarVisible:YES]; | |
[toolbar setVisible:YES]; | |
} | |
-(void)userLogout:(id)sender | |
{ | |
[i_userSessionController doLogout:self]; | |
} | |
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag | |
{ | |
if (anItemIdentifier == "toolItemUserName") | |
{ | |
var customView = [[CPView alloc] initWithFrame:CGRectMake(0,0,200,20)]; | |
labelUserName = [[CPTextField alloc] initWithFrame:CGRectMake(0,0,200,20)]; | |
searchField = [[CPSearchField alloc] initWithFrame:CGRectMake(0,20,200,32)]; | |
[labelUserName setStringValue:@""]; | |
[labelUserName setEditable:NO]; | |
[customView addSubview:labelUserName]; | |
[customView addSubview:searchField]; | |
toolItemUserName = [[CPToolbarItem alloc] initWithItemIdentifier:"toolItemUserName"]; | |
[toolItemUserName setView:customView]; | |
[toolItemUserName setLabel:@"Test"]; | |
[toolItemUserName setMinSize:CGSizeMake(200, 60)]; | |
[toolItemUserName setMaxSize:CGSizeMake(200, 60)]; | |
return toolItemUserName; | |
} | |
} | |
-(CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar | |
{ | |
return ["toolItemUserName"]; | |
} | |
-(CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar | |
{ | |
return ["toolItemUserName"]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Not Authenticated! | |
User is not authenticated | |
Loading CIB: Login.cib | |
Authenticated as: James Brink | |
User is authenticated null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation UserSessionController : CPObject | |
{ | |
CPTextField login; | |
CPSecureTextField password; | |
CPTextField status; | |
CPView view; | |
CPViewController viewController; | |
CPString m_userName @accessors(property=userName); | |
CPString m_firstName @accessors(property=firstName); | |
CPString m_lastName @accessors(property=lastName); | |
} | |
-(void)awakeFromCib | |
{ | |
[status setStringValue:@""]; | |
} | |
//returns the login view | |
-(CPViewController)loginView | |
{ | |
if (viewController == nil) | |
{ | |
viewController = [[CPViewController alloc] initWithCibName:@"Login" bundle:nil]; | |
console.debug("Loading CIB: Login.cib"); | |
return [viewController view]; | |
} | |
else | |
{ | |
return [viewController view]; | |
} | |
} | |
//Called to see if we are authenticated or not | |
-(void)checkAuth:(id)sender | |
{ | |
var request = [[CPURLRequest alloc] initWithURL:[CPURL URLWithString: @"/user_sessions/authenticated"]]; | |
[request setHTTPMethod:@"GET"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[CPURLConnection connectionWithRequest:request delegate:self]; | |
} | |
-(void)doLogin:(id)sender | |
{ | |
var UserSession = { | |
"user_session" : { | |
"login" : [login objectValue], | |
"password" : [password objectValue], | |
"remember_me" : false | |
} | |
}; | |
var request = [[CPURLRequest alloc] initWithURL:[CPURL URLWithString: @"/user_sessions"]]; | |
[request setHTTPMethod:@"POST"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setHTTPBody:[CPString JSONFromObject:UserSession]]; | |
response = [CPURLConnection connectionWithRequest:request delegate:self]; | |
} | |
-(void)doLogout:(id)sender | |
{ | |
var request = [[CPURLRequest alloc] initWithURL:[CPURL URLWithString: @"/logout"]]; | |
[request setHTTPMethod:@"GET"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[CPURLConnection connectionWithRequest:request delegate:self]; | |
} | |
-(void)doHelp:(id)sender | |
{ | |
} | |
//handle post response here | |
- (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data | |
{ | |
var result = JSON.parse(data); | |
if (result.auth) | |
{ | |
console.debug("Authenticated as: " + result.first_name + " " + result.last_name); | |
[self setFirstName:result.first_name]; | |
[self setLastName:result.last_name]; | |
[self setUserName:result.user_name]; | |
console.debug("Authenticated as: " + [self userName]); | |
[login setStringValue:@""]; | |
[password setStringValue:@""]; | |
[status setStringValue:@""]; | |
[[CPNotificationCenter defaultCenter] postNotificationName:@"userAuthenticated" object:self]; | |
} | |
else | |
{ | |
console.debug("Not Authenticated!"); | |
[status setStringValue:@"Authentication Failure!"]; | |
[[CPNotificationCenter defaultCenter] postNotificationName:@"userNotAuthenticated" object:self]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment