PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Business/Role.cs

#
C# | 515 lines | 345 code | 115 blank | 55 comment | 40 complexity | c31a32ca18ed2d5ede62406b1b0a73f0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. // Author: Joe Audette
  2. // Created: 2004-07-19
  3. // Last Modified: 2011-11-09
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.ObjectModel;
  14. using System.Collections.Generic;
  15. using System.Configuration;
  16. using System.Data;
  17. using mojoPortal.Data;
  18. using log4net;
  19. namespace mojoPortal.Business
  20. {
  21. /// <summary>
  22. /// Represents a user role.
  23. /// </summary>
  24. public class Role
  25. {
  26. private static readonly ILog log = LogManager.GetLogger(typeof(Role));
  27. public const string ContentAdministratorsRole = "Content Administrators";
  28. public const string AdministratorsRole = "Admins";
  29. #region Constructors
  30. public Role()
  31. {}
  32. public Role(int roleId)
  33. {
  34. GetRole(roleId);
  35. }
  36. public Role(int siteId, string roleName)
  37. {
  38. GetRole(siteId, roleName);
  39. }
  40. #endregion
  41. #region Private Properties
  42. private Guid roleGuid = Guid.Empty;
  43. private int roleID = -1;
  44. private int siteID = -1;
  45. private Guid siteGuid = Guid.Empty;
  46. private string roleName = string.Empty;
  47. private string displayName = string.Empty;
  48. //private bool enforceRelatedSitesMode = false;
  49. private static bool UseRelatedSiteMode
  50. {
  51. get
  52. {
  53. if (
  54. (ConfigurationManager.AppSettings["UseRelatedSiteMode"] != null)
  55. && (ConfigurationManager.AppSettings["UseRelatedSiteMode"] == "true")
  56. )
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. }
  63. private static int RelatedSiteID
  64. {
  65. get
  66. {
  67. int result = 1;
  68. if (ConfigurationManager.AppSettings["RelatedSiteID"] != null)
  69. {
  70. int.TryParse(ConfigurationManager.AppSettings["RelatedSiteID"], out result);
  71. }
  72. return result;
  73. }
  74. }
  75. #endregion
  76. #region Public Properties
  77. public int RoleId
  78. {
  79. get{return roleID;}
  80. }
  81. public Guid RoleGuid
  82. {
  83. get { return roleGuid; }
  84. }
  85. public Guid SiteGuid
  86. {
  87. get { return siteGuid; }
  88. set { siteGuid = value; }
  89. }
  90. public int SiteId
  91. {
  92. get
  93. {
  94. //if (UseRelatedSiteMode) { return RelatedSiteID; }
  95. return siteID;
  96. }
  97. set
  98. {
  99. //if (UseRelatedSiteMode)
  100. //{
  101. // siteID = RelatedSiteID;
  102. //}
  103. //else
  104. //{
  105. siteID = value;
  106. //}
  107. }
  108. }
  109. public string RoleName
  110. {
  111. get{return displayName;}
  112. set{displayName = value;}
  113. }
  114. public string DisplayName
  115. {
  116. get { return displayName; }
  117. }
  118. //public bool EnforceRelatedSitesMode
  119. //{
  120. // get { return enforceRelatedSitesMode; }
  121. // set { enforceRelatedSitesMode = value; }
  122. //}
  123. #endregion
  124. #region Private Methods
  125. private void GetRole(int roleId)
  126. {
  127. using (IDataReader reader = DBRoles.GetById(roleId))
  128. {
  129. if (reader.Read())
  130. {
  131. this.roleID = int.Parse(reader["RoleID"].ToString());
  132. this.siteID = int.Parse(reader["SiteID"].ToString());
  133. if (UseRelatedSiteMode) { siteID = RelatedSiteID; }
  134. this.roleName = reader["RoleName"].ToString();
  135. this.displayName = reader["DisplayName"].ToString();
  136. this.siteGuid = new Guid(reader["SiteGuid"].ToString());
  137. this.roleGuid = new Guid(reader["RoleGuid"].ToString());
  138. }
  139. }
  140. }
  141. private void GetRole(int siteId, string roleName)
  142. {
  143. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  144. using (IDataReader reader = DBRoles.GetByName(siteId, roleName))
  145. {
  146. if (reader.Read())
  147. {
  148. this.roleID = int.Parse(reader["RoleID"].ToString());
  149. this.siteID = int.Parse(reader["SiteID"].ToString());
  150. this.roleName = reader["RoleName"].ToString();
  151. this.displayName = reader["DisplayName"].ToString();
  152. this.siteGuid = new Guid(reader["SiteGuid"].ToString());
  153. this.roleGuid = new Guid(reader["RoleGuid"].ToString());
  154. }
  155. }
  156. }
  157. private bool Create()
  158. {
  159. int newID = 0;
  160. // role name never changes after creation
  161. // only display name is allowed to change
  162. // otherwise permissions to view pages
  163. // and edit modules would be orphaned when
  164. // a role was re-named
  165. if (UseRelatedSiteMode) { siteID = RelatedSiteID; }
  166. if(Exists(this.siteID, this.displayName))
  167. {
  168. //string errorMessage = ConfigurationManager.AppSettings["RoleExistsError"];
  169. //throw new Exception(errorMessage);
  170. // do nothing instead of throwing an exception
  171. }
  172. else
  173. {
  174. this.roleGuid = Guid.NewGuid();
  175. newID = DBRoles.RoleCreate(
  176. this.roleGuid,
  177. this.siteGuid,
  178. this.siteID,
  179. this.displayName);
  180. }
  181. if(newID > 0)
  182. {
  183. this.roleID = newID;
  184. this.roleName = this.displayName;
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. private bool Update()
  193. {
  194. return DBRoles.Update(this.roleID,this.displayName);
  195. }
  196. public bool Equals(string roleName)
  197. {
  198. bool result = false;
  199. if (roleName == this.roleName) result = true;
  200. return result;
  201. }
  202. #endregion
  203. #region Public Methods
  204. public bool Save()
  205. {
  206. if(this.roleID > -1)
  207. {
  208. return Update();
  209. }
  210. else
  211. {
  212. return Create();
  213. }
  214. }
  215. public bool HasUsers()
  216. {
  217. return (CountOfUsers() > 0);
  218. }
  219. public int CountOfUsers()
  220. {
  221. // TODO: implement actual select count from db
  222. // this is works but is not ideal
  223. int count = 0;
  224. using (IDataReader reader = GetRoleMembers(this.roleID))
  225. {
  226. while (reader.Read())
  227. {
  228. count += 1;
  229. }
  230. }
  231. return count;
  232. }
  233. public bool CanBeDeleted(List<string> rolesThatCannotBeDeleted)
  234. {
  235. if (rolesThatCannotBeDeleted != null)
  236. {
  237. foreach (string roleName in rolesThatCannotBeDeleted)
  238. {
  239. if (this.roleName == roleName) { return false; }
  240. if (this.displayName == roleName) { return false; }
  241. }
  242. }
  243. return true;
  244. }
  245. #endregion
  246. #region Static Methods
  247. public static IDataReader GetSiteRoles(int siteId)
  248. {
  249. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  250. return DBRoles.GetSiteRoles(siteId);
  251. }
  252. //public static Collection<Role> GetbySite(int siteId)
  253. //{
  254. // bool enforceRelatedSitesMode = false;
  255. // return GetbySite(siteId, enforceRelatedSitesMode);
  256. //}
  257. public static Collection<Role> GetbySite(int siteId)
  258. {
  259. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  260. Collection<Role> roles = new Collection<Role>();
  261. using (IDataReader reader = DBRoles.GetSiteRoles(siteId))
  262. {
  263. while (reader.Read())
  264. {
  265. Role role = new Role();
  266. role.roleID = Convert.ToInt32(reader["RoleID"]);
  267. role.siteID = Convert.ToInt32(reader["SiteID"]);
  268. role.displayName = reader["DisplayName"].ToString();
  269. role.roleName = reader["RoleName"].ToString();
  270. role.roleGuid = new Guid(reader["RoleGuid"].ToString());
  271. role.siteGuid = new Guid(reader["SiteGuid"].ToString());
  272. roles.Add(role);
  273. }
  274. }
  275. return roles;
  276. }
  277. public static Role GetRoleByName(int siteId, string roleName)
  278. {
  279. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  280. Role role = null;
  281. using (IDataReader reader = DBRoles.GetSiteRoles(siteId))
  282. {
  283. while (reader.Read())
  284. {
  285. string foundName = reader["RoleName"].ToString();
  286. if (foundName == roleName)
  287. {
  288. role = new Role();
  289. role.roleID = Convert.ToInt32(reader["RoleID"]);
  290. role.siteID = Convert.ToInt32(reader["SiteID"]);
  291. role.displayName = reader["DisplayName"].ToString();
  292. role.roleName = reader["RoleName"].ToString();
  293. role.roleGuid = new Guid(reader["RoleGuid"].ToString());
  294. role.siteGuid = new Guid(reader["SiteGuid"].ToString());
  295. }
  296. }
  297. }
  298. return role;
  299. }
  300. public static List<int> GetRoleIds(int siteId, string roleNamesSeparatedBySemiColons)
  301. {
  302. List<int> roleIds = new List<int>();
  303. List<string> roleNames = GetRolesNames(roleNamesSeparatedBySemiColons);
  304. foreach (string roleName in roleNames)
  305. {
  306. if (string.IsNullOrEmpty(roleName)) { continue; }
  307. Role r = Role.GetRoleByName(siteId, roleName);
  308. if (r == null)
  309. {
  310. log.Debug("could not get roleid for role named " + roleName);
  311. continue;
  312. }
  313. if (r.RoleId > -1) { roleIds.Add(r.RoleId); }
  314. }
  315. return roleIds;
  316. }
  317. public static List<string> GetRolesNames(string roleNamesSeparatedBySemiColons)
  318. {
  319. List<string> roleNames = new List<string>();
  320. string[] roles = roleNamesSeparatedBySemiColons.Split(';');
  321. foreach (string r in roles)
  322. {
  323. if (!roleNames.Contains(r)) { roleNames.Add(r); }
  324. }
  325. return roleNames;
  326. }
  327. public static int CountOfRoles(int siteId)
  328. {
  329. // TODO: implement actual select count from db
  330. // this is works but is not ideal
  331. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  332. //int count = 0;
  333. //using(IDataReader reader = GetSiteRoles(siteId))
  334. //{
  335. // while (reader.Read())
  336. // {
  337. // count += 1;
  338. // }
  339. //}
  340. //return count;
  341. return DBRoles.GetCountOfSiteRoles(siteId);
  342. }
  343. public static bool DeleteRole(int roleId)
  344. {
  345. return DBRoles.Delete(roleId);
  346. }
  347. public static bool Exists(int siteId, String roleName)
  348. {
  349. //if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  350. return DBRoles.Exists(siteId, roleName);
  351. }
  352. public static IDataReader GetRoleMembers(int roleId)
  353. {
  354. return DBRoles.GetRoleMembers(roleId);
  355. }
  356. public static IDataReader GetUsersNotInRole(int siteId, int roleId, int pageNumber, int pageSize, out int totalPages)
  357. {
  358. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  359. return DBRoles.GetUsersNotInRole(siteId, roleId, pageNumber, pageSize, out totalPages);
  360. }
  361. public static IDataReader GetUsersInRole(int siteId, int roleId, int pageNumber, int pageSize, out int totalPages)
  362. {
  363. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  364. return DBRoles.GetUsersInRole(siteId, roleId, pageNumber, pageSize, out totalPages);
  365. }
  366. public static IDataReader GetRolesUserIsNotIn(
  367. int siteId,
  368. int userId)
  369. {
  370. if (UseRelatedSiteMode) { siteId = RelatedSiteID; }
  371. return DBRoles.GetRolesUserIsNotIn(siteId, userId);
  372. }
  373. public static bool AddUser(
  374. int roleId,
  375. int userId,
  376. Guid roleGuid,
  377. Guid userGuid
  378. )
  379. {
  380. return DBRoles.AddUser(roleId,userId,roleGuid,userGuid);
  381. }
  382. public static bool RemoveUser(int roleId, int userId)
  383. {
  384. return DBRoles.RemoveUser(roleId,userId);
  385. }
  386. public static bool DeleteUserRoles(int userId)
  387. {
  388. return DBRoles.DeleteUserRoles(userId);
  389. }
  390. public static void AddUserToDefaultRoles(SiteUser siteUser)
  391. {
  392. Role role = new Role(siteUser.SiteId, "Authenticated Users");
  393. if (role.RoleId > 0)
  394. {
  395. Role.AddUser(role.RoleId, siteUser.UserId, role.RoleGuid, siteUser.UserGuid);
  396. }
  397. }
  398. #endregion
  399. }
  400. }