/// <summary> /// Allows to add a permission to a user directly /// </summary> /// <param name="p"></param> public static void AddEntityPermision(Type classType, string identifier, string userName) { if (logger.IsDebugEnabled) { logger.DebugFormat("Finding entity permission: {0} {1}", classType.Name, identifier); } EntityPermission ep = ControllerManager.EntityPermission.FindOrCreate(classType.Name, identifier); UserMember um = ControllerManager.UserMember.GetById(MembershipHelper.GetUser(userName).UserId); if (!ep.Users.Contains(um)) { if (logger.IsDebugEnabled) { logger.DebugFormat("Adding entity permission: {0} {1}", classType.Name, identifier); } ep.Users.Add(um); ControllerManager.EntityPermission.Save(ep); // Remove any cached information string cachedKey = string.Format("LISTIDENTIFIERS_{0}_{1}_{2}", classType.Name.ToString(), PermissionAction.Create.ToString(), MembershipHelper.GetUser(userName).UserId); object result = CacheManager.ExpireItem(cachedKey); } }
/// <summary> /// Validates the current object has permission to be executed. /// If there is no permission setup for this object and user, return false to enforce security. /// </summary> /// <param name="o">current object</param> /// <param name="action">action to be validated</param> /// <returns> /// True if it has the corresponding permission. /// False if no valid permissions are set for this object. /// </returns> public static bool Check(object o, Enum action) { MembershipHelperUser mu = MembershipHelper.GetUser(); if (mu == null) { return(false); } // Check if there are permissions for this object cached for this user. string checkAction = ((action != null) ? action.ToString() : ALLPERMISSIONS); string path = string.Empty; if ((typeof(HtmlControl).IsInstanceOfType(o) || typeof(WebControl).IsInstanceOfType(o)) && (o as Control) != null) { if ((o as Control).Page != null) { path = (o as Control).Page.AppRelativeVirtualPath; } else { path = (o as Control).AppRelativeTemplateSourceDirectory; } } if (logger.IsDebugEnabled) { logger.DebugFormat("Checking cache permission for: User:{0} Action:{1} Object:{2} Type:{3} Path:{4}", mu.UserId, checkAction, o, o.GetType().ToString(), path); } string cachedKey = string.Format("PERM_{0}_{1}_{2}_{3}_{4}", mu.UserId, o, checkAction, o.GetType().ToString(), path); object result = CacheManager.GetCached(cachedKey); if (result == null) { if (logger.IsDebugEnabled) { logger.Debug("Cache not found."); } // Obtain the list of permissions of the logged user List <Permission> lst = (ControllerManager.Permission.ListForCurrentUserAndSite(mu.UserId, action, Configuration.SiteCode) as List <Permission>); lst.AddRange(ControllerManager.Permission.ListPermisionsByUser(mu.UserId, Configuration.SiteCode)); if (logger.IsDebugEnabled) { logger.Debug("Reviewing list of possible permissions."); foreach (Permission p in lst) { logger.DebugFormat("Permission found: {0}", p.ToString()); } logger.Debug("End reviewing list of possible permissions."); } // Review which permission can be validated on the current object and validate each of them bool isChecked = false; foreach (Permission p in lst) { if (p.CanCheck(o) && p.Check(o, action)) { isChecked = true; } } CacheManager.AddItem(cachedKey, isChecked); if (logger.IsDebugEnabled) { logger.DebugFormat("Result: {0}", isChecked); } return(isChecked); } else { if (logger.IsDebugEnabled) { logger.Debug("Cache found."); logger.DebugFormat("Result: {0}", (bool)result); } return((bool)result); } }
public static IList GetPermissionIdentifiersFromFunction(Type classType, PermissionAction action) { string cachedKey = string.Format("LISTIDENTIFIERSFromFunction_{0}_{1}_{2}", classType.Name.ToString(), action.ToString(), MembershipHelper.GetUser().UserId); object result = CacheManager.GetCached(cachedKey); if (result == null) { result = ControllerManager.EntityPermission.ListIdentifiersFromFunction(classType, MembershipHelper.GetUser().UserId, (PermissionAction)action); CacheManager.AddItem(cachedKey, result); } return((IList)result); }
public static IList GetPermissionIdentifiers(Type classType, PermissionAction action) { // Check if there are permissions for this object cached for this user. if (logger.IsDebugEnabled) { logger.DebugFormat("Finding entity permission list: {0} {1}", classType.Name, action); } string cachedKey = string.Format("LISTIDENTIFIERS_{0}_{1}_{2}", classType.Name.ToString(), action.ToString(), MembershipHelper.GetUser().UserId); object result = CacheManager.GetCached(cachedKey); if (result == null) { result = ControllerManager.EntityPermission.ListIdentifiers(classType, MembershipHelper.GetUser().UserId, (PermissionAction)action); CacheManager.AddItem(cachedKey, result); } return((IList)result); }