Issue
I have a CakePHP app, and there are currently 2 user_type(s).
- admin (administrator)
- account (normal user)
I am working on something where users can pay an extra fee, and it gives them access to more content and pages. I was going to make a new user_type called PRO and use that, but the PRO members still need access to all the things people with the account user_type have access to.
Basically, what is the easiest way to set something up where I can give users permissions to view certain things and pages using similar code to what I am currently using:
<?php if( $currentUser['user_type'] == 'account' ) { ?>
and <?php } ?>
for restricting content
if( $this->User['user_type'] == 'account')) {
switch ($this->request->getParam('action')) {
case 'PAGE':
return true;
default:
return false;
break;
}
}
for controlling page access
Also, I need an easy way to give users access via a frontend admin panel. I currently use the below code, and I would need a way to adapt it to work for this new "PRO" option.
if( $user->user_type != 'admin' && $currentUser['id'] = 1 )
echo $this->Form->postLink(__('Make Admin'), ['action' => 'make_admin', $user->id], ['confirm' => __('Are you sure you want to make admin?', $user->name), 'class'=>'btn btn-danger btn-md']);
else
echo $this->Form->postLink(__('Revoke Admin'), ['action' => 'revoke_admin', $user->id], ['confirm' => __('Are you sure you want to remove admin rights?', $user->name), 'class'=>'btn btn-danger btn-md']);
I am very much a noob at cakephp, so any help is greatly appreciated. I didn't make the base app, I hired a dev to do it, so I am learning cakephp so I can work on adapting the app to fit my needs better. So far, this community has been very helpful and I hope I can continue to get everything working!
Solution
My perspective is I will do all changes in AppController and put the permission from there. Then, I can handle easily rest of the condition without hassle.
For example inside AppController beforeFilter()
$this->authenticatedUser = AuthComponent::user();
//$this->authenticatedUser['superadmin'] = false; //if you set true, this will be your root user
and set your new role here
$this->authenticatedUser['role'] = 'PRO' //or your `account` with your condition
If you change role account
to PRO
role, you can set $this->authenticatedUser['role']
to set your new role.
if(in_array($this->authenticatedUser['role'],['account','PRO'])) {
switch ($this->request->getParam('action')) {
case 'PAGE':
return true;
default:
return false;
break;
}
}
or you can change something like
if(!$this->authenticatedUser['superadmin'])
or if($this->authenticatedUser['superadmin'])
If I can see more of your AppController (BaseController) and I can help you more. Now, there is a lot of unknowns and made a lot of assumptions for help. Anyway, my approach may be easily help you to add new roles.
Answered By - 502_Geek
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.