Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow authsomeLogin to return falsish values
This way you can just return an empty array if you don't have a guest
account. Returning null as a value will still throw an exception.
  • Loading branch information
felixge committed Dec 26, 2009
1 parent e2cbd34 commit b096bc6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
24 changes: 11 additions & 13 deletions README.md
Expand Up @@ -25,16 +25,15 @@ Load authsome in your AppController and specify the name of your user model:
);
}

Implement authsomeLogin in your user model. For a minimal setup you just need to be able to return a guest user array or lookup a user using some sort of credentials.
Implement authsomeLogin in your user model (must return a non-null value):

class User extends AppModel{
public function authsomeLogin($type, $credentials = array()) {
switch ($type) {
case 'guest':
// You could also create a guest user here if it doesn't
// exist, or simply return a hard coded user array.
$conditions = array('User.id' => YOUR_GUEST_ID);
break;
// You can return any non-null value here, if you don't
// have a guest account, just return an empty array
return array('it' => 'works');
case 'credentials':
$password = Authsome::hash($credentials['password']);

Expand All @@ -45,19 +44,19 @@ Implement authsomeLogin in your user model. For a minimal setup you just need to
);
break;
default:
return false;
return null;
}

return $this->find('first', compact('conditions'));
}
}

Almost done! Check if you did everything right so far by verifying that you can access the guest account information anywhere in your app:
Almost done! Check if you did everything right so far by putting this in one of your controllers:

$guest = Authsome::get();
debug($guest);

If this returns your guest account as configured in your `authsomeLogin` login function, you can go ahead and implement a simple login function:
If this returns `Array([it] => works)`, you can go ahead and implement a simple login function:

class UsersController extends AppController{
public function login() {
Expand Down Expand Up @@ -166,10 +165,9 @@ If so, proceed to the next step and add the `'cookie'` login `$type` to your aut
public function authsomeLogin($type, $credentials = array()) {
switch ($type) {
case 'guest':
// You could also create a guest user here if it doesn't
// exist, or simply return a hard coded user array.
$conditions = array('User.id' => YOUR_GUEST_ID);
break;
// You can return any non-null value here, if you don't
// have a guest account, just return an empty array
return array('it' => 'works');
case 'credentials':
$password = Authsome::hash($credentials['password']);

Expand Down Expand Up @@ -206,7 +204,7 @@ If so, proceed to the next step and add the `'cookie'` login `$type` to your aut
);
break;
default:
return false;
return null;
}

return $this->find('first', compact('conditions'));
Expand Down
6 changes: 1 addition & 5 deletions controllers/components/authsome.php
Expand Up @@ -84,10 +84,6 @@ public function login($type = 'credentials', $credentials = null) {

$user = $userModel->authsomeLogin($type, $credentials);

if ($user === false) {
return false;
}

Configure::write($this->settings['configureKey'], $user);
$this->Session->write($this->settings['sessionKey'], $user);
return $user;
Expand Down Expand Up @@ -146,7 +142,7 @@ private function __getActiveUser() {
$this->__useGuestAccount();

$user = Configure::read($this->settings['configureKey']);
if (!$user) {
if (is_null($user)) {
throw new Exception(
'Unable to initilize user'
);
Expand Down

0 comments on commit b096bc6

Please sign in to comment.