Hey,
Thought I would post a bit more information on this...note I am just grabbing bits of code and mashing it together here as I don't want to copy all my code here:
1) Update index.php to setup your acl rules
- Code: Select all
// Load the ACL Rules - See the example for info on doing this...
include $config['SITE_PATH'] . 'protected/config/acl.conf.php';
...
Doo::acl('DooRbAcl')->rules = $acl; // Load RbAcl Module and define the rules
Doo::acl()->defaultFailedRoute = '/authentication-error';
2) Have a BaseController. Mil0s posted a good example starting point for this on the learn doophp blog. Note: I often refer to it as a BaseController which is just the terminology I'm more used to but its the same idea as the CoreController. Find the code here:
http://learn.doophp.com/2009/09/concept ... framework/3) In my BaseController I added some code to my BaseController..there is more but this is what you need for this task:
- Code: Select all
Doo::loadClass('auth/MyUserClass'); // Class defined above...change to something more user friendly
Doo::loadCore('DooController');
class BaseController extends DooController {
public function beforeRun($resource, $action) {
parent::beforeRun($resource, $action);
$this->checkUserObjectInSessionOrCreateNew();
// See if user has access to called controller / action
return $this->checkUserHasAccessTo($resource, $action);
}
/**
* Checks to see if the user already has an active session and recovers there user object
* otherwise it will create a new user object and assign it to there session
*
* The object can then be used to authenticate a user, log them out and check there roles
*/
private function checkUserObjectInSessionOrCreateNew() {
// Disable URL Session ID's
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
// Set the session namespace and start it
Doo::session('MyAppNameSpace'); // Change to suitable namespace for your application
// If they do not have a user object specified we will create a new one (anonymous)
if (isset(Doo::session()->user) == false) {
Doo::session()->user = new MyUserClass();
}
}
/**
* Check if the user's role is able to access the resource/action.
*
* @param string $resource Resource name (use Controller class name)
* @param string $action Action name (use Method name)
* @return array|string Returns the fail route if user cannot access the resource
*/
private function checkUserHasAccessTo($resource, $action) {
if(($rs = $this->acl()->process(Doo::session()->user->getRoles(), $resource, $action ))){
return $rs;
}
}
}
4) Thats all you really need (just make sure you setup the user class from my earlier post correctly
5) I have then added a renderAction() function I can call from any of the child classes so I can use layouts etc and setup access to translations etc...I also have this set a $data['user'] = Doo::session()->user so I can then make use of the user object in my templates...
6) in your template you can do things like:
- Code: Select all
{% user->hasRole('some-role') %}
You have the role some-role!
{% endif %}
Hope this helps get things moving a little more for you.
You will then need to setup your own controller like AccountController and setup some actions in here so you will probably want:
- Register
- Login
- Logout
- MyAccount
- RecoverPassword
The login function would then need to call:
- Code: Select all
Doo::session()->user->login($username, $password, $rememberMe);
The logout function would then need to call:
- Code: Select all
Doo::session()->user = null;
Doo::session()->destroy();
You would also likely redirect the user onto there 'home page' or the site homepage after completing these tasks
Oh and you need to define your user table and have your MyUserClass define the login function so something like:
- Code: Select all
public function login($username, $password, $rememberMe=false) {
// You would check all this against your db and populate things from here but leave this to you
if ($username = 'Demo' && $password = 'p@55w0rd') {
$this->userId = 1;
$this->username = $username;
$this->name = 'Demo User';
$this->userRoles = array('role1', 'role2', 'role3');
$this->isLoggedIn = true;
return true;
}
return false;
}
Good luck!
Richard
Note: code samples my not be 100% accurate.