Results 1 to 6 of 6

Thread: [RESOLVED] Convertion issue of user roles.

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Resolved [RESOLVED] Convertion issue of user roles.

    I am trying to convert asp roles (not asp question) to an enum.
    The enum is this one:
    Code:
        [Serializable]
        [Flags]
        public enum SiteRoles
        {
            User = 1 << 0,
            Admin = 1 << 1,
            Helpdesk = 1 << 2
        }
    }
    doing this, i get the user roles: HttpContext.Current.Session["role"] = Roles.GetRolesForUser(userName);
    The below show how the roles are written in the session variable:

    Code:
    HttpContext.Current.Session["role"]
    {string[1]}
        [0]: "Helpdesk"
    What i want to do is this:
    Code:
    SiteRoles role = (SiteRoles)httpContext.Session["role"];
    This will issue an invalid cast. Can anyone tell me how to convert the above?
    I tried with an array but again got cannot convert variable to SiteRoles;
    Code:
    string[] s = null;
    s = (string[])httpContext.Session["role"];
    SiteRoles role = (SiteRoles)s;
    As you know, anything having to do with string will immediately turn me to a way below average programmer(let alone this c# thing) so any help would be appreciated.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Convertion issue of user roles.

    Another suggestion (as i thing i will switch to dictionary or something, it's frustrating!!)
    Let's say i do this:
    Code:
    string[] s = null;
    s = (string[])httpContext.Session["role"]; // what we will have here is an array of roles
     foreach (string element in s)
        {
        // either create an new enum and compare or something similar(?) or find if element satisfy the condition.
          
        }
    Let's say that
    Roles have:
    ?Roles
    Admin | Helpdesk

    and the element only have "Admin" in the array.

    So i must find a way to compare the upcoming array with the Roles condition (Admin | Helpdesk)
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Convertion issue of user roles.

    Best i could come up with yesterday. Will look at this today also:
    Code:
       var MyProperty = (SiteRoles)Enum.Parse(typeof(SiteRoles), s[someinteger]);
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Convertion issue of user roles.

    How about writing like this:
    Code:
     HttpContext.Current.Session["role"] = (int) Roles.GetRolesForUser(userName);
    and then reading like this:
    Code:
    SiteRoles role = (SiteRoles) (int) httpContext.Session["role"];

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Convertion issue of user roles.

    Hey JM. I get cannot convert type string[] to int.
    So let me explain better what the issue is here:

    In my authorize class i override the Roles property
    Code:
    public new SiteRoles Roles;
    This will give me whatever (since you know MVC) property roles i want for a authorized sub in a controller
    Code:
      [CustomAuthorize(Roles = SiteRoles.Admin | SiteRoles.User)]
            public ActionResult Aboutm()
            {
                return View();
            }
    So the problem here is that i may or may not have the entire SiteRoles enum.
    That is my main problem since i cannot iterate trough the Roles "enum" as i could do with SiteRoles
    Code:
    var values = Enum.GetValues(typeof(Sitemap)); //will work but:
    var values = Enum.GetValues(typeof(Roles.GetType)); // will not work.
    So what i did was to put my session roles in an array of string:
    Code:
    string[] sarroles = null;
    sarroles = (string[])httpContext.Session["role"];
    This will help me do a foreach to check against the Roles enum:

    Code:
    int x =0;
    int y = 0;
    foreach (string element in sarroles)
    {
        var MyProperty = (SiteRoles)Enum.Parse(typeof(SiteRoles), sarroles[x]);      
        if (Roles != 0 && (Roles & MyProperty) != MyProperty)
        {
            FailedRolesAuth = true;
         //   return false;
            continue;
        }
      
        //found role, exit for.
        FailedRolesAuth = false;
        break;
        x++;
        y++;
    }
    Now my main problem is here:
    Code:
     if (Roles != 0 && (Roles & MyProperty) != MyProperty)
    This passes ok if my first role coming from element exists in the Roles enum but it will not pass ok (aka it will go inside the if (Roles != 0 && (Roles & MyProperty) != MyProperty) {...} ).
    I am trying as another solution to iterate the Roles enum so i can do a double loop and compare one to one with the MyProperty string but the problem is that Roles, get to be a "half enum", meaning it is not treated as a type and i cannot just put SiteRoles since Roles may not contain all the values of SiteRoles.

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Convertion issue of user roles.

    GOD DA#, SH(T, F**K, AAAAAAA!!!!
    I forgot an x++; inside the first loop. Fffff...
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width