PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] CakePHP Multiple User Roles/PRO Membership Role

 February 12, 2022     authorization, cakephp, cakephp-3.x, php     No comments   

Issue

I have a CakePHP app, and there are currently 2 user_type(s).

  1. admin (administrator)
  2. 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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing