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

  • SEARCH
  • Home
  • 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 8864921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:22:26+00:00 2026-06-14T16:22:26+00:00

I am doing my first app that contains permissions and roles with multiple users.

  • 0

I am doing my first app that contains permissions and roles with multiple users.
My understanding is the best way to do this is using BitWise formatting. Is this true, or is there a better alternative?

This is my current Test Code, and I’m getting a rather bizzare effect.
If someone could shed some light as to why this is happening the way it is, that would be greatly appreciated.

EXAMPLE:

$user = array('permissions' => 1); // This *should* return ONLY READ, (except it shows all)
$user = array('permissions' => 8); // Shows correct, FULL resources.

You can use this to test the code… http://writecodeonline.com/php/

define("PERM_R", 1);  # Read    
define("PERM_W", 2);  # Write   
define("PERM_E", 4);  # Edit    
define("PERM_D", 8);  # Delete  

define("ROLE_GUEST",  PERM_R);
define("ROLE_EDITOR", ROLE_GUEST | PERM_W | PERM_E);
define("ROLE_FULL",   ROLE_EDITOR | PERM_D);

function hasAccess($user, $action) {
    if( is_array($user) ) {
        return $user['permissions'] & $action;
    } else if ( is_int($user) ) {
        return $user & $action;
    }
}


$user = array('permissions' => 1);
echo "USER PERMISSIONS: ". $user['permissions'] ."<br /><br />";

# TEST PERMS
if(hasAccess($user, PERM_R)) {
    echo PERM_R;
    echo " - Yes you can see READ <br />";
}
if(hasAccess($user, PERM_R | PERM_W)) {
    echo PERM_R | PERM_W;
    echo " - Yes you can see READ & WRITE<br />";
}
if(hasAccess($user, PERM_R | PERM_W | PERM_E)) {
    echo PERM_R | PERM_W | PERM_E;
    echo " - Yes you can see READ & WRITE & EDIT<br />";
}
if(hasAccess($user, PERM_R | PERM_W | PERM_E | PERM_D)) {
    echo PERM_R | PERM_W | PERM_E | PERM_D;
    echo " - Yes you can see READ & WRITE & EDIT & DELETE<br />";
}

# TEST ROLES
if(hasAccess($user, ROLE_GUEST)) {
    echo ROLE_GUEST;
    echo " - Yes, You Are A GUEST <br />";
}
if(hasAccess($user, ROLE_EDITOR)) {
    echo ROLE_EDITOR;
    echo " - Yes, You Are A EDITOR <br />";
}
if(hasAccess($user, ROLE_FULL)) {
    echo ROLE_FULL;
    echo " - Yes, You Are A FULL <br />";
}
  • 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-06-14T16:22:27+00:00Added an answer on June 14, 2026 at 4:22 pm

    The issue is that you’re simply returning the results of a bitwise & which, for all of your tests, is at least 1/true because all users have the R permission.

    1. Only feed one permission into hasAccess(), stop using |.
    2. Write a new function to determine the user level that does an integer comparison.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this app of mine that contains a section that will allow the
First off, I'm relatively new to Google App Engine, so I'm probably doing something
i'm doing my first applications using JDBC/Oracle... Today i had a problem and i
For my first Silverlight app, I've written a program that sends user-supplied search strings
I have an app that has a main window that contains a bunch of
I have this web app that is running ASP .NET MVC 1.0 with LINQ
I know almost nothing about linq. I'm doing this: var apps = from app
Hopefully a very simple question. But I'm using Code First with an MVC app
I'm writing my first 2D app for Android using OpenGL. I'm writing it on
I am using code first and EF 4.1 im my MVC3 App and have

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.