/Less/SRV/SecuritySRV.cs

http://lessframework.codeplex.com · C# · 245 lines · 229 code · 13 blank · 3 comment · 24 complexity · 0c4189f3fe478426429a5aa0de59c7d7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using Less.DAL;
  4. using Less.Core.Model;
  5. using Less.Tools;
  6. namespace Less.SRV
  7. {
  8. public class SecuritySRV:IReadSecurityService, IWriteSecurityService
  9. {
  10. readonly UsersDAL usersDAL;
  11. readonly RolesDAL rolesDAL;
  12. readonly UsersRolesDAL usersRolesDAL;
  13. readonly IdentifiersDAL identifiersDAL;
  14. public SecuritySRV()
  15. {
  16. usersDAL = new UsersDAL();
  17. rolesDAL = new RolesDAL();
  18. usersRolesDAL = new UsersRolesDAL();
  19. identifiersDAL = new IdentifiersDAL();
  20. }
  21. public SecuritySRV(string connectionStringName)
  22. {
  23. usersDAL = new UsersDAL(new Database(connectionStringName));
  24. rolesDAL = new RolesDAL(new Database(connectionStringName));
  25. usersRolesDAL = new UsersRolesDAL(new Database(connectionStringName));
  26. identifiersDAL = new IdentifiersDAL(new Database(connectionStringName));
  27. }
  28. public SecuritySRV(IDatabase database)
  29. {
  30. usersDAL = new UsersDAL(database);
  31. rolesDAL = new RolesDAL(database);
  32. usersRolesDAL = new UsersRolesDAL(database);
  33. identifiersDAL = new IdentifiersDAL(database);
  34. }
  35. public List<User> GetAllUsers()
  36. {
  37. return usersDAL.SelectAll();
  38. }
  39. public List<Role> GetAllRoles()
  40. {
  41. return rolesDAL.SelectAll();
  42. }
  43. public void InsertUser(User user)
  44. {
  45. if (SelectByEmail(user.Email) != null || SelectByName(user.UserName) != null )
  46. {
  47. throw new RecordAlreadyExistsException(user.Serialize());
  48. }
  49. else
  50. {
  51. user.CreationDate = DateTime.Now;
  52. usersDAL.Insert(user);
  53. }
  54. }
  55. public void UpdateUser(User user)
  56. {
  57. if ( SelectByEmail(user.Email) == null && SelectByName(user.UserName) == null )
  58. {
  59. throw new RecordNotFoundException(user.Serialize());
  60. }
  61. else
  62. {
  63. usersDAL.Update(user);
  64. }
  65. }
  66. public void DeleteUser(User user)
  67. {
  68. usersDAL.Delete(user);
  69. }
  70. public User SelectByName(string username)
  71. {
  72. return usersDAL.SelectByName(username);
  73. }
  74. public string GetPassword(string username, string answer)
  75. {
  76. return usersDAL.GetPassword(username,answer);
  77. }
  78. public User SelectById(int providerUserKey)
  79. {
  80. return usersDAL.SelectById(providerUserKey);
  81. }
  82. public User SelectByEmail(string email)
  83. {
  84. return usersDAL.SelectByEmail(email);
  85. }
  86. public User SelectByIdentifier(string identifier)
  87. {
  88. return usersDAL.SelectByIdentifier(identifier);
  89. }
  90. public string CreateIdentifier(int userId, DateTime? expiryDate)
  91. {
  92. Identifier identifier = new Identifier();
  93. identifier.UserId = userId;
  94. identifier.GUID = Guid.NewGuid().ToString();
  95. identifier.CreationDate = DateTime.Now;
  96. identifier.ExpiryDate = expiryDate??DateTime.Now.AddDays(1);
  97. identifier.UsedDate = null;
  98. identifiersDAL.Insert(identifier);
  99. return identifier.GUID;
  100. }
  101. public Identifier SelectIdentifierByGUID(string identifier)
  102. {
  103. return identifiersDAL.SelectByGUID(identifier);
  104. }
  105. public void ConsumeUserIdentifier(int id)
  106. {
  107. Identifier identifier = identifiersDAL.SelectById(id);
  108. identifier.UsedDate = DateTime.Now;
  109. identifiersDAL.Update(identifier);
  110. }
  111. public int GetNumberOfUsersOnline(DateTime addMinutes)
  112. {
  113. return usersDAL.GetNumberOfUsersOnline(addMinutes);
  114. }
  115. public List<User> GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
  116. {
  117. return usersDAL.SelectPage(pageIndex, pageSize, out totalRecords);
  118. }
  119. public bool IsUserInRole(string username, string roleName)
  120. {
  121. bool exists = false;
  122. // OPTI: use an INNER JOIN
  123. User user = usersDAL.SelectByName(username);
  124. if ( user != null )
  125. {
  126. Role role = rolesDAL.SelectByName(roleName);
  127. if ( role != null )
  128. {
  129. exists = usersRolesDAL.IsUserInRole(user.Id, role.Id);
  130. }
  131. }
  132. return exists;
  133. }
  134. public List<Role> SelectRolesByUserName(string username)
  135. {
  136. return rolesDAL.SelectRolesByUserName(username);
  137. }
  138. public void InsertRoleByName(string roleName)
  139. {
  140. Role role = rolesDAL.SelectByName(roleName);
  141. if ( role == null )
  142. {
  143. role = new Role { Name = roleName };
  144. rolesDAL.Insert(role);
  145. }
  146. else
  147. {
  148. throw new RecordAlreadyExistsException(roleName);
  149. }
  150. }
  151. public bool DeleteRoleByName(string roleName)
  152. {
  153. Role role = rolesDAL.SelectByName(roleName);
  154. if ( role != null )
  155. {
  156. role = new Role { Name = roleName };
  157. rolesDAL.Delete(role);
  158. return true;
  159. }
  160. else
  161. {
  162. throw new RecordNotFoundException(roleName);
  163. }
  164. }
  165. public void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
  166. {
  167. // OPTI: use an inner join
  168. var rolesDAL = new RolesDAL();
  169. var usersDAL = new UsersDAL();
  170. foreach ( string roleName in roleNames )
  171. {
  172. Role role = rolesDAL.SelectByName(roleName);
  173. if ( role != null )
  174. {
  175. foreach ( string username in usernames )
  176. {
  177. User user = usersDAL.SelectByName(username);
  178. if ( user != null )
  179. {
  180. UserRole userRole = usersRolesDAL.SelectByUserIdRoleId(user.Id, role.Id);
  181. usersRolesDAL.Delete(userRole);
  182. }
  183. else
  184. {
  185. Log.Warning(Msg.NotFound(username));
  186. }
  187. }
  188. }
  189. else
  190. {
  191. Log.Warning(Msg.NotFound(roleName));
  192. ;
  193. }
  194. }
  195. }
  196. public List<User> SelectUsersByRoleName(string roleName)
  197. {
  198. return usersDAL.SelectUsersByRoleName(roleName);
  199. }
  200. public void AddUsersToRoles(string[] usernames, string[] roleNames)
  201. {
  202. // OPTI: use an inner join
  203. var rolesDAL = new RolesDAL();
  204. var usersDAL = new UsersDAL();
  205. foreach ( string roleName in roleNames )
  206. {
  207. Role role = rolesDAL.SelectByName(roleName);
  208. if ( role != null )
  209. {
  210. foreach ( string username in usernames )
  211. {
  212. User user = usersDAL.SelectByName(username);
  213. if ( user != null )
  214. {
  215. var userRole = new UserRole { UserId = user.Id, RoleId = role.Id };
  216. usersRolesDAL.Insert(userRole);
  217. }
  218. else
  219. {
  220. Log.Warning(Msg.NotFound(username));
  221. }
  222. }
  223. }
  224. else
  225. {
  226. Log.Warning(Msg.NotFound(roleName));
  227. ;
  228. }
  229. }
  230. }
  231. }
  232. }