PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/RoleService.cs

#
C# | 322 lines | 218 code | 41 blank | 63 comment | 40 complexity | f612be2d4162abe9ffc727b1f815aece MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace App_Code
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web.Script.Services;
  7. using System.Web.Security;
  8. using System.Web.Services;
  9. using BlogEngine.Core;
  10. using BlogEngine.Core.Json;
  11. /// <summary>
  12. /// Membership service to support AJAX calls
  13. /// </summary>
  14. [WebService(Namespace = "http://tempuri.org/")]
  15. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16. [ScriptService]
  17. public sealed class RoleService : WebService
  18. {
  19. #region Constants and Fields
  20. #endregion
  21. #region Constructors and Destructors
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="RoleService"/> class.
  24. /// </summary>
  25. public RoleService()
  26. {
  27. }
  28. #endregion
  29. #region Public Methods
  30. /// <summary>
  31. /// Adds a role.
  32. /// </summary>
  33. /// <param name="roleName">
  34. /// The role name.
  35. /// </param>
  36. /// <returns>
  37. /// JSON Response
  38. /// </returns>
  39. [WebMethod]
  40. public JsonResponse Add(string roleName)
  41. {
  42. if (!Security.IsAuthorizedTo(Rights.CreateNewRoles))
  43. {
  44. return GetNotAuthorized();
  45. }
  46. else if (Utils.StringIsNullOrWhitespace(roleName))
  47. {
  48. return new JsonResponse() { Message = Resources.labels.roleNameIsRequired };
  49. }
  50. else if (Roles.RoleExists(roleName))
  51. {
  52. return new JsonResponse() { Message = string.Format(Resources.labels.roleAlreadyExists, roleName) };
  53. }
  54. else
  55. {
  56. var response = new JsonResponse();
  57. try
  58. {
  59. Roles.CreateRole(roleName);
  60. response.Success = true;
  61. response.Message = string.Format(Resources.labels.roleHasBeenCreated, roleName);
  62. }
  63. catch (Exception ex)
  64. {
  65. Utils.Log(string.Format("Roles.AddRole: {0}", ex.Message));
  66. response.Success = false;
  67. response.Message = string.Format(Resources.labels.couldNotCreateRole, roleName);
  68. }
  69. return response;
  70. }
  71. }
  72. /// <summary>
  73. /// The delete.
  74. /// </summary>
  75. /// <param name="id">
  76. /// The role id.
  77. /// </param>
  78. /// <returns>
  79. /// JSON Response.
  80. /// </returns>
  81. [WebMethod]
  82. public JsonResponse Delete(string id)
  83. {
  84. if (!Security.IsAuthorizedTo(Rights.DeleteRoles))
  85. {
  86. return GetNotAuthorized();
  87. }
  88. else if (Utils.StringIsNullOrWhitespace(id))
  89. {
  90. return new JsonResponse() { Message = Resources.labels.roleNameIsRequired };
  91. }
  92. try
  93. {
  94. Right.OnRoleDeleting(id);
  95. Roles.DeleteRole(id);
  96. return new JsonResponse() { Success = true, Message = string.Format(Resources.labels.roleHasBeenDeleted, id) };
  97. }
  98. catch (Exception ex)
  99. {
  100. Utils.Log(string.Format("Roles.DeleteRole: {0}", ex.Message));
  101. return new JsonResponse() { Message = string.Format(Resources.labels.couldNotDeleteRole, id) };
  102. }
  103. }
  104. /// <summary>
  105. /// Saves the rights for a specific Role.
  106. /// </summary>
  107. /// <param name="roleName">The name of the role whose rights are being updated.</param>
  108. /// <param name="rightsCollection">A dictionary of rights that a role is allowed to have.</param>
  109. /// <returns></returns>
  110. [WebMethod]
  111. public JsonResponse SaveRights(string roleName, Dictionary<string, bool> rightsCollection)
  112. {
  113. if (!Security.IsAuthorizedTo(Rights.EditRoles))
  114. {
  115. return new JsonResponse() { Message = Resources.labels.notAuthorized };
  116. }
  117. else if (Utils.StringIsNullOrWhitespace(roleName) || !Roles.RoleExists(roleName))
  118. {
  119. return new JsonResponse() { Message = Resources.labels.invalidRoleName };
  120. }
  121. else if (rightsCollection == null)
  122. {
  123. return new JsonResponse() { Message = Resources.labels.rightsCanNotBeNull };
  124. }
  125. else
  126. {
  127. // The rights collection can be empty, just not null. An empty array would indicate that a role is
  128. // being updated to include no rights at all.
  129. // Remove spaces from each key (i.e. so "Edit Own User" becomes EditOwnUser).
  130. rightsCollection = rightsCollection.ToDictionary(r => r.Key.Replace(" ", string.Empty), r => r.Value, StringComparer.OrdinalIgnoreCase);
  131. // Validate the dictionary before doing any altering to Rights.
  132. foreach (var right in rightsCollection)
  133. {
  134. if (!Right.RightExists(right.Key))
  135. {
  136. return new JsonResponse() { Success = false, Message = String.Format(Resources.labels.noRightExists, right.Key) };
  137. }
  138. else if (right.Value == false)
  139. {
  140. return new JsonResponse() { Success = false, Message = Resources.labels.doNotPassRightsWithFalseValue };
  141. }
  142. }
  143. foreach (var right in Right.GetAllRights())
  144. {
  145. if (right.Flag != Rights.None)
  146. {
  147. if (rightsCollection.ContainsKey(right.Name))
  148. {
  149. right.AddRole(roleName);
  150. }
  151. else
  152. {
  153. right.RemoveRole(roleName);
  154. }
  155. }
  156. }
  157. BlogEngine.Core.Providers.BlogService.SaveRights();
  158. return new JsonResponse() { Success = true, Message = string.Format(Resources.labels.rightsUpdatedForRole, roleName) };
  159. }
  160. }
  161. /// <summary>
  162. /// Edits a role.
  163. /// </summary>
  164. /// <param name="id">
  165. /// The row id.
  166. /// </param>
  167. /// <param name="bg">
  168. /// The background.
  169. /// </param>
  170. /// <param name="vals">
  171. /// The values.
  172. /// </param>
  173. /// <returns>
  174. /// JSON Response.
  175. /// </returns>
  176. [WebMethod]
  177. public JsonResponse Edit(string id, string bg, string[] vals)
  178. {
  179. if (!Security.IsAuthorizedTo(Rights.EditRoles))
  180. {
  181. return GetNotAuthorized();
  182. }
  183. else if (Utils.StringIsNullOrWhitespace(id))
  184. {
  185. return new JsonResponse() { Message = Resources.labels.idArgumentNull };
  186. }
  187. else if (vals == null)
  188. {
  189. return new JsonResponse() { Message = Resources.labels.valsArgumentNull };
  190. }
  191. else if (vals.Length == 0 || Utils.StringIsNullOrWhitespace(vals[0]))
  192. {
  193. return new JsonResponse() { Message = Resources.labels.roleNameIsRequired };
  194. }
  195. var response = new JsonResponse();
  196. try
  197. {
  198. Right.OnRenamingRole(id, vals[0]);
  199. string[] usersInRole = Roles.GetUsersInRole(id);
  200. if (usersInRole.Length > 0)
  201. {
  202. Roles.RemoveUsersFromRoles(usersInRole, new string[] { id });
  203. }
  204. Roles.DeleteRole(id);
  205. Roles.CreateRole(vals[0]);
  206. if (usersInRole.Length > 0)
  207. {
  208. Roles.AddUsersToRoles(usersInRole, new string[] { vals[0] });
  209. }
  210. Right.RefreshAllRights();
  211. response.Success = true;
  212. response.Message = string.Format(Resources.labels.roleUpdatedFromTo, id, vals[0]);
  213. }
  214. catch (Exception ex)
  215. {
  216. Utils.Log(string.Format("Roles.UpdateRole: {0}", ex.Message));
  217. response.Message = string.Format(Resources.labels.couldNotUpdateRole, vals[0]);
  218. }
  219. return response;
  220. }
  221. /// <summary>
  222. /// Returns the default rights for the role.
  223. /// </summary>
  224. /// <param name="roleName">The roleName.</param>
  225. /// <returns>
  226. /// JSON Response containing delimited default rights.
  227. /// </returns>
  228. [WebMethod]
  229. public JsonResponse GetDefaultRoleRights(string roleName)
  230. {
  231. if (!Security.IsAuthorizedTo(Rights.EditRoles))
  232. {
  233. return GetNotAuthorized();
  234. }
  235. else if (Utils.StringIsNullOrWhitespace(roleName))
  236. {
  237. return new JsonResponse() { Message = Resources.labels.roleNameArgumentNull };
  238. }
  239. List<Rights> defaultRights = Right.GetDefaultRights(roleName);
  240. var response = new JsonResponse()
  241. {
  242. Success = true,
  243. Data = string.Join("|", defaultRights.Select(r => Utils.FormatIdentifierForDisplay(r.ToString())).ToArray())
  244. };
  245. return response;
  246. }
  247. /// <summary>
  248. /// Returns the rights for the role.
  249. /// </summary>
  250. /// <param name="roleName">The roleName.</param>
  251. /// <returns>
  252. /// JSON Response containing delimited rights.
  253. /// </returns>
  254. [WebMethod]
  255. public JsonResponse GetRoleRights(string roleName)
  256. {
  257. if (!Security.IsAuthorizedTo(Rights.EditRoles))
  258. {
  259. return GetNotAuthorized();
  260. }
  261. else if (Utils.StringIsNullOrWhitespace(roleName))
  262. {
  263. return new JsonResponse() { Message = Resources.labels.roleNameArgumentNull };
  264. }
  265. IEnumerable<Right> roleRights = Right.GetRights(roleName);
  266. var response = new JsonResponse()
  267. {
  268. Success = true,
  269. Data = string.Join("|", roleRights.Select(r => r.DisplayName).ToArray())
  270. };
  271. return response;
  272. }
  273. #endregion
  274. #region Methods
  275. private static JsonResponse GetNotAuthorized()
  276. {
  277. return new JsonResponse() { Success = false, Message = Resources.labels.notAuthorized };
  278. }
  279. #endregion
  280. }
  281. }