Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6379947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:12:14+00:00 2026-05-25T02:12:14+00:00

How do I check if the currently logged in user belongs to the admin

  • 0

How do I check if the currently logged in user belongs to the admin role.

I have two tables, a users and roles table. In the users table I have a foreign key called role_id. And a role of admin is ID of 1 in the roles table.

1.) How would I do this check in the view to show an admin link

2.) How would I do this check in the app_controller to prevent access to all actions that have the admin prefix?

I have tried something like:

public function beforeRender()
{
    $user = $this->Auth->user();

    if (!empty($user))
    {
        $user = $user[$this->Auth->getModel()->alias];
    }
    $this->set(compact('user'));


    if($user['Role']['id'] == 1)
    {
        $is_admin = true;
    }
}

and then I try and use the is_admin variable to check around the app

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:12:14+00:00Added an answer on May 25, 2026 at 2:12 am

    one way of doing this is setting a variable in your controller functions

       function beforeFilter()
        {
        if($this->Auth->user('role_id')==1){
        $this->set("role",$this->Auth->user('role_id'));//it will set a variable role for your view 
         }
    else
    {
    $this->set("role",2);//2 is the role of normal users
    
    }
    
        }
    

    in your view you test this variable like below

        <?php if($role==1){ ?>
          echo $html->link('view registered users',array('controller'=>'users','action'=>'admin_check_users'),array('title'=>'Users'));/provide a link for admin using html helper;   }
        else{
         echo $html->link('logout',array('controller'=>'users','action'=>'logout'),array('title'=>'Logout...'));//provide a link for normal users using html helper;   
    
    }
    ?>
    

    for your 2nd answer… you can do the same…

          function beforeFilter()
        {
        if($this->Auth->user('role_id')==1){
         $this->Auth->allow('admin_view','admin_controls');//put your all admin actions separated by comma
    
         }
    
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have code in my ApplicationController to check if a user is logged
how can I check if a user (not the one currently logged in) is
I have the following function which retrives the currently logged in users' username and
I'm using Magento 1.5, and I need to check whether the currently-logged-in user is
Have SubscriberList When an order is placed I want to check if New User's
How can i check that the current user logged onto the machine is an
So currently in our code, we have a bunch of classes around accessing roles
Currently I use the following for checking is a user is logged in: function
Currently I am using ctype_alnum to check for alphanumeric type in csv file. But
I'm currently writing an Eclipse APT plug-in to check if my annotations are correct.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.