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 6957301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:59:00+00:00 2026-05-27T14:59:00+00:00

Trying to manage access to a web site I created some necessary entities The

  • 0

Trying to manage access to a web site I created some necessary entities
enter image description here

The goal is use a custom permission attribute for some controller’s action method of my MVC application.

[Permissions(PermissionType.SomePermissionName, CrudType.CanDelete)]
public ActionResult SomeAction()
{
}

For this operation I have two enums

[Flags]
public enum CrudType
{
    CanCreate = 0x1,
    CanRead = 0x2,
    CanUpdate = 0x4,
    CanDelete = 0x8,
}

[Flags]
public enum PermissionType
{
   SomePermissionName = 0x1,
   //... 
}

Now I want the method below to check permissions

public static bool CanAccess(RolePermissions rp, CrudType crudType)
{
    var pInfo = rp.GetType().GetProperties();
    var res = pInfo.FirstOrDefault(x => x.Name == crudType.ToString());
    if(res != null)
    {
        return Convert.ToBoolean(res.GetValue(rp, null));
    }
    return false;
}

It works good but is it safe to use reflection here? Is it a good style?
One more question is about such piece of code

var permission = PermissionService.GetByName(permissionType.ToString());

Here I’m trying to get a permission object from a database using some named constant from the PermissionType enum.
In both cases the correct work depends on relationships between enums and some table fields or records. On other side I have a good mechanism of controlling logic (as it seems to me).
Is that a good way?

  • 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-27T14:59:01+00:00Added an answer on May 27, 2026 at 2:59 pm

    ANOTHER EDIT
    In your case it would make sense to create a readonly property ExistingPermissions for the RolePermissions class, and do the merging of the four booleans into one CrudType within that property getter. Then you can just do rp.ExistingPermissions.HasFlag(permissionToCheck).

    EDITED

    Thanks to @DevDelivery for pointing out the issue – good catch. Unfortunately the fixed solution isn’t as pretty as I was hoping for, so in this case it might make sense to go with @DevDelivery’s approach.

    Since you have your CrudType as “bitfields”, you can use a cleaner approach (less code and better readability):

    public static bool CanAccess(RolePermissions rp, CrudType permissionToCheck)
    {
        CrudType existingPermissions = 
                                    SetPermissionFlag(CrudType.CanCreate, rp.CanCreate) |
                                    SetPermissionFlag(CrudType.CanRead, rp.CanRead) | 
                                    SetPermissionFlag(CrudType.CanUpdate, rp.CanUpdate) |
                                    SetPermissionFlag(CrudType.CanDelete, rp.CanDelete);
    
        return existingPermissions.HasFlag(permissionToCheck);
    }
    
    public static CrudType SetPermissionFlag(CrudType crudType, bool permission)
    {
        return (CrudType)((int)crudType * Convert.ToInt32(permission));
    }
    

    The drawback compared to your solution is that you will have to modify this method in case you add more operations (to the existing CanRead, etc.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How delphi7 access C# .net managed dll ? i'm trying to access some DLL
I'm trying to manually manage some geometry (spatial) columns in a rails model. When
I'm trying to use couchapp to manage my CouchDB application, but it's missing a
I'm trying to make a web app that will manage my Mercurial repositories for
Basically I'm trying to create a SessionManager class which I can use to manage
I'm trying to use some @Service annotated classes (yes, using the mvc:annotation-driven) within the
So, I've been trying to give my web application read access to files in
I'm trying to use decorators in order to manage the way users may or
I am trying to access my web application protected by SSL from an Android
Im trying to access a web service from a remote computer. I managed to

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.