PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Src/Fwk/Xenta.Domains/Core/Operations/AccountMap.cs

http://xenta.codeplex.com
C# | 424 lines | 263 code | 65 blank | 96 comment | 25 complexity | 7144a1df5b41b683fb9d8619ca1375d2 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NHibernate.Linq;
  5. using Xenta.Api;
  6. using Xenta.Entities;
  7. using Xenta.Enums;
  8. using Xenta.Security;
  9. using Xenta.Utils;
  10. namespace Xenta.Operations
  11. {
  12. /// <summary>
  13. /// Maps the account to role.
  14. /// </summary>
  15. public class MapAccountToRole : Operation<Boolean>
  16. {
  17. #region Ctors
  18. /// <summary>
  19. /// Initializes a new class instance.
  20. /// </summary>
  21. public MapAccountToRole()
  22. {
  23. AccountID = 0;
  24. RoleID = 0;
  25. }
  26. #endregion
  27. #region Properties
  28. /// <summary>
  29. /// Gets or sets the account identifier.
  30. /// </summary>
  31. public int AccountID
  32. {
  33. get;
  34. set;
  35. }
  36. /// <summary>
  37. /// Gets or sets the role identifier.
  38. /// </summary>
  39. public int RoleID
  40. {
  41. get;
  42. set;
  43. }
  44. #endregion
  45. #region Methods
  46. /// <summary>
  47. /// Executes the operation.
  48. /// </summary>
  49. /// <param name="uow">The unit of work.</param>
  50. public override void Execute(IUnitOfWork uow)
  51. {
  52. _result = false;
  53. var account = uow.Query<AccountEntity>()
  54. .Where(x => x.EntityID == AccountID)
  55. .FetchMany(x => x.Roles)
  56. .SingleOrDefault();
  57. if(account != null)
  58. {
  59. if(account.Roles.Any(x => x.EntityID == RoleID))
  60. {
  61. _result = true;
  62. }
  63. else
  64. {
  65. var role = uow.Execute(new GetRole
  66. {
  67. EntityID = RoleID
  68. }).Result;
  69. if(role != null)
  70. {
  71. account.Roles.Add(role);
  72. uow.Update(account);
  73. _result = true;
  74. }
  75. }
  76. }
  77. }
  78. #endregion
  79. }
  80. /// <summary>
  81. /// Unmaps the account from role.
  82. /// </summary>
  83. public class UnmapAccountFromRole : Operation
  84. {
  85. #region Ctors
  86. /// <summary>
  87. /// Initializes a new class instance.
  88. /// </summary>
  89. public UnmapAccountFromRole()
  90. {
  91. AccountID = 0;
  92. RoleID = 0;
  93. }
  94. #endregion
  95. #region Properties
  96. /// <summary>
  97. /// Gets or sets the account identifier.
  98. /// </summary>
  99. public int AccountID
  100. {
  101. get;
  102. set;
  103. }
  104. /// <summary>
  105. /// Gets or sets the role identifier.
  106. /// </summary>
  107. public int RoleID
  108. {
  109. get;
  110. set;
  111. }
  112. #endregion
  113. #region Methods
  114. /// <summary>
  115. /// Executes the operation.
  116. /// </summary>
  117. /// <param name="uow">The unit of work.</param>
  118. public override void Execute(IUnitOfWork uow)
  119. {
  120. var account = uow.Query<AccountEntity>()
  121. .Where(x => x.EntityID == AccountID)
  122. .FetchMany(x => x.Roles)
  123. .SingleOrDefault();
  124. if(account != null)
  125. {
  126. var role = account.Roles.SingleOrDefault(x => x.EntityID == RoleID);
  127. if(role != null)
  128. {
  129. account.Roles.Remove(role);
  130. uow.Update(account);
  131. }
  132. }
  133. }
  134. #endregion
  135. }
  136. /// <summary>
  137. /// Gets the account-role map.
  138. /// </summary>
  139. public class GetAccountRoleMap : Operation<IDictionary<String, Boolean>>,
  140. ISecuredOperation, IApiOperation, IDisclosable
  141. {
  142. #region Ctors
  143. /// <summary>
  144. /// Initializes a new class instance.
  145. /// </summary>
  146. public GetAccountRoleMap()
  147. : base(new Dictionary<String, Boolean>())
  148. {
  149. AccountID = 0;
  150. }
  151. #endregion
  152. #region Properties
  153. /// <summary>
  154. /// Gets the collection of security policies.
  155. /// </summary>
  156. public IEnumerable<SecurityPolicy> SecurityPolicies
  157. {
  158. get
  159. {
  160. yield return new SecurityPolicy()
  161. .IsAuthenticated()
  162. .HasPermission("ACCNT");
  163. }
  164. }
  165. /// <summary>
  166. /// Gets or sets the account identifier.
  167. /// </summary>
  168. public int AccountID
  169. {
  170. get;
  171. set;
  172. }
  173. #endregion
  174. #region Methods
  175. /// <summary>
  176. /// Executes the operation.
  177. /// </summary>
  178. /// <param name="uow">The unit of work.</param>
  179. public override void Execute(IUnitOfWork uow)
  180. {
  181. var query = uow.Query<RoleEntity>().Select(x => new
  182. {
  183. x.Code,
  184. Mapped = x.Accounts.Any(i => i.EntityID == AccountID)
  185. });
  186. foreach(var i in query.ToList())
  187. _result.Add(i.Code, i.Mapped);
  188. }
  189. /// <summary>
  190. /// Discloses the result.
  191. /// </summary>
  192. /// <param name="level">The disclosure level.</param>
  193. /// <returns>The result object.</returns>
  194. public object Disclose(DisclosureLevel level)
  195. {
  196. return _result;
  197. }
  198. #endregion
  199. }
  200. /// <summary>
  201. /// Maps the account to role.
  202. /// </summary>
  203. public class MapAccountToRoleByCode : Operation<Boolean>,
  204. ISecuredOperation, IApiOperation, IDisclosable
  205. {
  206. #region Ctors
  207. /// <summary>
  208. /// Initializes a new class instance.
  209. /// </summary>
  210. public MapAccountToRoleByCode()
  211. {
  212. AccountID = 0;
  213. RoleCode = String.Empty;
  214. }
  215. #endregion
  216. #region Properties
  217. /// <summary>
  218. /// Gets the collection of security policies.
  219. /// </summary>
  220. public IEnumerable<SecurityPolicy> SecurityPolicies
  221. {
  222. get
  223. {
  224. yield return new SecurityPolicy()
  225. .IsAuthenticated()
  226. .HasPermission("ACCNT");
  227. }
  228. }
  229. /// <summary>
  230. /// Gets or sets the account identifier.
  231. /// </summary>
  232. public int AccountID
  233. {
  234. get;
  235. set;
  236. }
  237. /// <summary>
  238. /// Gets or sets the role code.
  239. /// </summary>
  240. public string RoleCode
  241. {
  242. get;
  243. set;
  244. }
  245. #endregion
  246. #region Methods
  247. /// <summary>
  248. /// Executes the operation.
  249. /// </summary>
  250. /// <param name="uow">The unit of work.</param>
  251. public override void Execute(IUnitOfWork uow)
  252. {
  253. _result = false;
  254. var account = uow.Query<AccountEntity>()
  255. .Where(x => x.EntityID == AccountID)
  256. .FetchMany(x => x.Roles)
  257. .SingleOrDefault();
  258. if(account != null)
  259. {
  260. if(account.Roles.Any(x => x.Code.Equals(RoleCode)))
  261. {
  262. _result = true;
  263. }
  264. else
  265. {
  266. var role = uow.Execute(new GetRoleByCode
  267. {
  268. Code = RoleCode
  269. }).Result;
  270. if(role != null)
  271. {
  272. account.Roles.Add(role);
  273. uow.Update(account);
  274. _result = true;
  275. }
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// Discloses the result.
  281. /// </summary>
  282. /// <param name="level">The disclosure level.</param>
  283. /// <returns>The result object.</returns>
  284. public object Disclose(DisclosureLevel level)
  285. {
  286. return _result;
  287. }
  288. #endregion
  289. }
  290. /// <summary>
  291. /// Unmaps the account from role.
  292. /// </summary>
  293. public class UnmapAccountFromRoleByCode : Operation,
  294. ISecuredOperation, IApiOperation
  295. {
  296. #region Ctors
  297. /// <summary>
  298. /// Initializes a new class instance.
  299. /// </summary>
  300. public UnmapAccountFromRoleByCode()
  301. {
  302. AccountID = 0;
  303. RoleCode = String.Empty;
  304. }
  305. #endregion
  306. #region Properties
  307. /// <summary>
  308. /// Gets the collection of security policies.
  309. /// </summary>
  310. public IEnumerable<SecurityPolicy> SecurityPolicies
  311. {
  312. get
  313. {
  314. yield return new SecurityPolicy()
  315. .IsAuthenticated()
  316. .HasPermission("ACCNT");
  317. }
  318. }
  319. /// <summary>
  320. /// Gets or sets the account identifier.
  321. /// </summary>
  322. public int AccountID
  323. {
  324. get;
  325. set;
  326. }
  327. /// <summary>
  328. /// Gets or sets the role identifier.
  329. /// </summary>
  330. public string RoleCode
  331. {
  332. get;
  333. set;
  334. }
  335. #endregion
  336. #region Methods
  337. /// <summary>
  338. /// Executes the operation.
  339. /// </summary>
  340. /// <param name="uow">The unit of work.</param>
  341. public override void Execute(IUnitOfWork uow)
  342. {
  343. var account = uow.Query<AccountEntity>()
  344. .Where(x => x.EntityID == AccountID)
  345. .FetchMany(x => x.Roles)
  346. .SingleOrDefault();
  347. if(account != null)
  348. {
  349. var role = account.Roles.SingleOrDefault(x => x.Code.Equals(RoleCode));
  350. if(role != null)
  351. {
  352. account.Roles.Remove(role);
  353. uow.Update(account);
  354. }
  355. }
  356. }
  357. #endregion
  358. }
  359. }