/// <summary> /// Gets single user permissions object /// </summary> /// <param name="userId">User Id of user</param> /// <returns>Instance of Permissions object</returns> public static Permissions GetUserPermissions(int userId) { Permissions permissions = new Permissions(); PropertyInfo[] fields = permissions.GetType().GetProperties(); KMBit.DAL.chargebitEntities db = null; try { db = new chargebitEntities(); Admin_Users au = (from u in db.Admin_Users where u.User_Id==userId select u).FirstOrDefault<Admin_Users>(); if(au!=null && au.IsSuperAdmin) { foreach (PropertyInfo f in fields) { f.SetValue(permissions, true); } return permissions; } List<Admin_Actions> actions = (from a in db.Admin_Actions select a).ToList<Admin_Actions>(); List<Admin_Users_Actions> userActions = (from ua in db.Admin_Users_Actions where ua.User_Id == userId select ua).ToList<Admin_Users_Actions>(); if (userActions != null && userActions.Count > 0) { foreach (Admin_Users_Actions ua in userActions) { Admin_Actions action = (from a in actions where a.Id == ua.Action_Id select a).FirstOrDefault<Admin_Actions>(); if (action != null) { foreach (PropertyInfo f in fields) { if (f.Name == action.Name || au.IsSuperAdmin) { f.SetValue(permissions, true); } } } } } } catch { } finally { if(db!=null) { db.Dispose(); } } return permissions; }
public void GrantUserPermissions(int userId,Permissions permission) { if(userId==0) { throw new KMBitException(""); } List<Admin_Actions> actions = new List<Admin_Actions>(); using (chargebitEntities db = new chargebitEntities()) { List<Admin_Actions> allActions = (from ac in db.Admin_Actions select ac).ToList<Admin_Actions>(); PropertyInfo[] props = permission.GetType().GetProperties(); foreach(PropertyInfo prop in props) { bool hasPermission = (bool)prop.GetValue(permission); if(hasPermission) { Admin_Actions ac = (from acc in allActions where acc.Name == prop.Name select acc).FirstOrDefault<Admin_Actions>(); if(ac!=null) { actions.Add(ac); } } } } GrantUserPermissions(userId, actions); }