c# - How to read access rights from access mask? -


user can have following access rights:

read = 1 create = 2 edit = 4 delete = 8 publish = 16 administer = 32 

when access rights saved in database, 1 number used represent access rights user.

e.g.

3 = read + create 25 = read + delete + publish 

how can access rights given number (access mask)?

any appreciated!

var mask = (accessrights)25;  var rightsformask = enum.getvalues(typeof(accessrights))                         .cast<accessrights>()                         .where(x => mask.hasflag(x));  foreach (var right in rightsformask) {     // displays "1:read", "8:delete", "16:publish"     console.writeline((int)right + ":" + right); }  // ...  [flags] public enum accessrights {     read = 1, create = 2, edit = 4, delete = 8, publish = 16, administer = 32 } 

if you're not using .net4 hasflag method won't available, in case you'll need change where clause read where(x => (mask & x) == x).


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -