/src/BugNET.BLL/RoleManager.cs

# · C# · 355 lines · 201 code · 49 blank · 105 comment · 47 complexity · 3be98245b6bfebff91a5fe278bc01efc MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using BugNET.DAL;
  5. using BugNET.Entities;
  6. using BugNET.Common;
  7. using log4net;
  8. namespace BugNET.BLL
  9. {
  10. public static class RoleManager
  11. {
  12. private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  13. /// <summary>
  14. /// Saves the role object
  15. /// </summary>
  16. /// <returns><c>true</c> if successful</returns>
  17. public static bool SaveOrUpdate(Role entity)
  18. {
  19. if (entity == null) throw new ArgumentNullException("entity");
  20. if (entity.ProjectId <= Globals.NEW_ID) throw (new ArgumentException("Cannot save role, the project id is invalid"));
  21. if (string.IsNullOrEmpty(entity.Name)) throw (new ArgumentException("The role name cannot be empty or null"));
  22. if (entity.Id > Globals.NEW_ID)
  23. return DataProviderManager.Provider.UpdateRole(entity);
  24. var tempId = DataProviderManager.Provider.CreateNewRole(entity);
  25. if (tempId <= 0) return false;
  26. entity.Id = tempId;
  27. return true;
  28. }
  29. private const string RolePermissionCache = "RolePermission";
  30. /// <summary>
  31. /// Associates the default roles created at installation to a project.
  32. /// </summary>
  33. /// <param name="projectId">The project id.</param>
  34. public static void CreateDefaultProjectRoles(int projectId)
  35. {
  36. if (projectId <= Globals.NEW_ID)
  37. throw (new ArgumentOutOfRangeException("projectId"));
  38. foreach (var role in Globals.DefaultRoles)
  39. {
  40. var r = new Role { ProjectId = projectId, Name = role, Description = role, AutoAssign = false};
  41. var newRoleId = DataProviderManager.Provider.CreateNewRole(r);
  42. int[] permissions = null;
  43. //add permissions to roles
  44. switch (role)
  45. {
  46. case "Project Administrators":
  47. permissions = Globals.AdministratorPermissions;
  48. break;
  49. case "Read Only":
  50. permissions = Globals.ReadOnlyPermissions;
  51. break;
  52. case "Reporter":
  53. permissions = Globals.ReporterPermissions;
  54. break;
  55. case "Developer":
  56. permissions = Globals.DeveloperPermissions;
  57. break;
  58. case "Quality Assurance":
  59. permissions = Globals.QualityAssurancePermissions;
  60. break;
  61. }
  62. if (permissions != null)
  63. foreach (var i in permissions)
  64. {
  65. AddPermission(newRoleId, i);
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Get all roles by project
  71. /// </summary>
  72. /// <param name="projectId"></param>
  73. /// <returns>List of role objects</returns>
  74. public static List<Role> GetByProjectId(int projectId)
  75. {
  76. if (projectId <= Globals.NEW_ID)
  77. throw (new ArgumentOutOfRangeException("projectId"));
  78. return DataProviderManager.Provider.GetRolesByProject(projectId);
  79. }
  80. /// <summary>
  81. /// Gets the role by id.
  82. /// </summary>
  83. /// <param name="roleId">The role id.</param>
  84. /// <returns></returns>
  85. public static Role GetById(int roleId)
  86. {
  87. if (roleId <= Globals.NEW_ID)
  88. throw (new ArgumentOutOfRangeException("roleId"));
  89. return DataProviderManager.Provider.GetRoleById(roleId);
  90. }
  91. /// <summary>
  92. /// Creates the role.
  93. /// </summary>
  94. /// <param name="roleName">Name of the role.</param>
  95. /// <param name="projectId">The project id.</param>
  96. /// <param name="description">The description.</param>
  97. /// <param name="autoAssign">if set to <c>true</c> [auto assign].</param>
  98. /// <returns></returns>
  99. public static int CreateRole(string roleName, int projectId, string description, bool autoAssign)
  100. {
  101. if (!Exists(roleName, projectId))
  102. {
  103. var r = new Role {ProjectId = projectId, Name = roleName, Description = description, AutoAssign = autoAssign};
  104. SaveOrUpdate(r);
  105. return r.Id;
  106. }
  107. return 0;
  108. }
  109. /// <summary>
  110. /// Roles the exists.
  111. /// </summary>
  112. /// <param name="roleName">Name of the role.</param>
  113. /// <param name="projectId">The project id.</param>
  114. /// <returns></returns>
  115. public static bool Exists(string roleName, int projectId)
  116. {
  117. if (projectId <= 0) throw new ArgumentOutOfRangeException("projectId");
  118. if (String.IsNullOrEmpty(roleName)) throw new ArgumentOutOfRangeException("roleName");
  119. return DataProviderManager.Provider.RoleExists(roleName, projectId);
  120. }
  121. /// <summary>
  122. /// Gets the roles for user.
  123. /// </summary>
  124. /// <param name="userName">Name of the user.</param>
  125. /// <param name="projectId">The project id.</param>
  126. /// <returns></returns>
  127. public static List<Role> GetForUser(string userName, int projectId)
  128. {
  129. if (String.IsNullOrEmpty(userName)) throw new ArgumentOutOfRangeException("userName");
  130. if (!HttpContext.Current.User.Identity.IsAuthenticated)
  131. return DataProviderManager.Provider.GetRolesByUserName(userName, projectId);
  132. // performance enhancement
  133. // WRH 2012-04-06
  134. // use the current loaded user roles if we are looking at the same user
  135. return userName.ToLower().Equals(HttpContext.Current.User.Identity.Name.ToLower()) ?
  136. CurrentUserRoles.FindAll(p => p.ProjectId == projectId) :
  137. DataProviderManager.Provider.GetRolesByUserName(userName, projectId);
  138. }
  139. /// <summary>
  140. /// Gets the roles for user.
  141. /// </summary>
  142. /// <param name="userName">Name of the user.</param>
  143. /// <returns></returns>
  144. public static List<Role> GetForUser(string userName)
  145. {
  146. if (String.IsNullOrEmpty(userName)) throw new ArgumentOutOfRangeException("userName");
  147. if (!HttpContext.Current.User.Identity.IsAuthenticated)
  148. return DataProviderManager.Provider.GetRolesByUserName(userName);
  149. // performance enhancement
  150. // WRH 2012-04-06
  151. // use the current loaded user roles if we are looking at the same user
  152. return userName.ToLower().Equals(HttpContext.Current.User.Identity.Name.ToLower()) ?
  153. CurrentUserRoles :
  154. DataProviderManager.Provider.GetRolesByUserName(userName);
  155. }
  156. /// <summary>
  157. /// Gets all roles.
  158. /// </summary>
  159. /// <returns></returns>
  160. public static List<Role> GetAll()
  161. {
  162. return DataProviderManager.Provider.GetAllRoles();
  163. }
  164. /// <summary>
  165. /// Adds a user to a role
  166. /// </summary>
  167. /// <param name="userName">Name of the user.</param>
  168. /// <param name="roleId">The role id.</param>
  169. public static void AddUser(string userName, int roleId)
  170. {
  171. if (String.IsNullOrEmpty(userName)) throw new ArgumentOutOfRangeException("userName");
  172. if (roleId <= Globals.NEW_ID) throw new ArgumentOutOfRangeException("roleId");
  173. DataProviderManager.Provider.AddUserToRole(userName, roleId);
  174. HttpContext.Current.Cache.Remove(RolePermissionCache);
  175. }
  176. /// <summary>
  177. /// Removes a user from a role
  178. /// </summary>
  179. /// <param name="userName">Name of the user.</param>
  180. /// <param name="roleId">The role id.</param>
  181. public static void RemoveUser(string userName, int roleId)
  182. {
  183. if (String.IsNullOrEmpty(userName)) throw new ArgumentOutOfRangeException("userName");
  184. if (roleId <= 0) throw new ArgumentOutOfRangeException("roleId");
  185. DataProviderManager.Provider.RemoveUserFromRole(userName, roleId);
  186. HttpContext.Current.Cache.Remove(RolePermissionCache);
  187. }
  188. /// <summary>
  189. /// Deletes the role.
  190. /// </summary>
  191. /// <param name="roleId">The role id.</param>
  192. /// <returns></returns>
  193. public static bool Delete(int roleId)
  194. {
  195. if (roleId <= Globals.NEW_ID) throw new ArgumentOutOfRangeException("roleId");
  196. if (DataProviderManager.Provider.DeleteRole(roleId))
  197. {
  198. HttpContext.Current.Cache.Remove(RolePermissionCache);
  199. return true;
  200. }
  201. return false;
  202. }
  203. /// <summary>
  204. /// Retreives the Role Permissions DataView from the cache if exists, otherwise loads
  205. /// it into the cache
  206. /// </summary>
  207. /// <returns>Role Permissions DataView</returns>
  208. private static List<RolePermission> GetPermissions()
  209. {
  210. var permissions = (List<RolePermission>)HttpContext.Current.Cache[RolePermissionCache];
  211. if (permissions == null)
  212. {
  213. permissions = DataProviderManager.Provider.GetRolePermissions();
  214. HttpContext.Current.Cache.Insert(RolePermissionCache, permissions);
  215. }
  216. return permissions;
  217. }
  218. /// <summary>
  219. /// Checks the Role Permission DataView if a permission row exists
  220. /// </summary>
  221. /// <param name="projectId"></param>
  222. /// <param name="role"></param>
  223. /// <param name="permissionKey"></param>
  224. /// <returns>[true] if row exists</returns>
  225. public static bool HasPermission(int projectId, string role, string permissionKey)
  226. {
  227. //check if the role for a project has permission
  228. var permission = GetPermissions().Find(
  229. p => p.ProjectId == projectId && p.RoleName == role && p.PermissionKey == permissionKey);
  230. return permission != null;
  231. }
  232. /// <summary>
  233. /// Gets all permissions by role
  234. /// </summary>
  235. /// <param name="roleId">The role id.</param>
  236. /// <returns>List of permission objects</returns>
  237. public static IEnumerable<BugNET.Entities.Permission> GetPermissionsById(int roleId)
  238. {
  239. if (roleId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("roleId"));
  240. return DataProviderManager.Provider.GetPermissionsByRoleId(roleId);
  241. }
  242. /// <summary>
  243. /// Deletes a permission object from a role
  244. /// </summary>
  245. /// <param name="roleId">The role id.</param>
  246. /// <param name="permissionId">The permission id.</param>
  247. /// <returns>[true] if successful</returns>
  248. public static bool DeletePermission(int roleId, int permissionId)
  249. {
  250. if (roleId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("roleId"));
  251. if (permissionId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("permissionId"));
  252. if (DataProviderManager.Provider.DeletePermission(roleId, permissionId))
  253. {
  254. HttpContext.Current.Cache.Remove(RolePermissionCache);
  255. return true;
  256. }
  257. return false;
  258. }
  259. /// <summary>
  260. /// Adds a permission object to a role
  261. /// </summary>
  262. /// <param name="roleId">The role id.</param>
  263. /// <param name="permissionId">The permission id.</param>
  264. /// <returns>[true] if successful</returns>
  265. public static bool AddPermission(int roleId, int permissionId)
  266. {
  267. if (roleId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("roleId"));
  268. if (permissionId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("permissionId"));
  269. if (DataProviderManager.Provider.AddPermission(roleId, permissionId))
  270. {
  271. HttpContext.Current.Cache.Remove(RolePermissionCache);
  272. return true;
  273. }
  274. return false;
  275. }
  276. private const string CURRENT_USER_ROLES = "CURRENT_USER_ROLES";
  277. /// <summary>
  278. /// performance enhancement
  279. /// WRH 2012-04-06
  280. /// Singleton pattern for the current users roles
  281. /// We load them the first time and keep them around for the lenght of the request
  282. /// </summary>
  283. private static List<Role> CurrentUserRoles
  284. {
  285. get
  286. {
  287. var ctx = HttpContext.Current;
  288. if (ctx == null) return null;
  289. var items = ctx.Items[CURRENT_USER_ROLES] as List<Role>;
  290. if(items == null)
  291. {
  292. var roles = DataProviderManager.Provider.GetRolesByUserName(ctx.User.Identity.Name);
  293. CurrentUserRoles = roles;
  294. return roles;
  295. }
  296. return items;
  297. }
  298. set
  299. {
  300. var ctx = HttpContext.Current;
  301. if (ctx == null) return;
  302. ctx.Items[CURRENT_USER_ROLES] = value;
  303. }
  304. }
  305. }
  306. }