Tuesday, 15 March 2011

Adding multiple roles to CakePHP Auth Component -



Adding multiple roles to CakePHP Auth Component -

i using tutorial add together authentication , authorization component app.

the problem tutorials find assume user have 1 role. in attached video, roles enum field in users table. have 3 tables:

the users table roles table responsibilities table a table link roles , responsibilities a table link users , roles

i have log in process working, getting roles logged in user. best method this? know of tutorial includes well? using cake 2.5.2

i can post ever code might think relevant, have pretty much whats in video. also, lot of inner working of code seems hidden. suggestions great!

thanks

edit

below models user/roles/responsibilities tables added users object:

**user table:** id (pk) first_name last_name username password **role table** id(pk) role_name **responsibility table** id(pk) responsibility_name **user_roles_membership** id(pk) role_id(fk) user_id(fk) **roles_responsibilities_membership** id(pk) roles_id(fk) responsibility_id(fk)

simple cake authentication , authorization

to reply in detail require me write whole code or exhaustive article - i'm not going that. instead i'll point in right direction.

when user logged in, fetch associated roles , responsibilities user. next code taken from part of documentation (read whole page!). have no clue how info associated i'm guessing here.

$this->auth->authenticate = array( authcomponent::all => array( 'contain' => array( 'responsibility', 'role' ) ), 'form' );

look @ session after user logged in now, should see additional info there. utilize debugkit for illustration or debug() session.

next thing write customized authorization handler work data. documentation shows how here.

app::uses('baseauthorize', 'controller/component/auth'); class myauthorize extends baseauthorize { public function authorize($user, cakerequest $request) { // things permission scheme here. } }

inside authorize() method add together whatever logic need check permissions current logged in user, passed first arg, , check them against whatever want check in request, passed 2nd arg.

all of pretty straight forwards , documented on page. again, recommend read whole page. should become obvious how done.

also might want inquire more specific question generic 1 in case encounter problems implementation.

cakephp cakephp-2.3

No comments:

Post a Comment