/trunk/Src/Fwk/Xenta.Domains/Core/Operations/AccountMap.cs
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NHibernate.Linq;
- using Xenta.Api;
- using Xenta.Entities;
- using Xenta.Enums;
- using Xenta.Security;
- using Xenta.Utils;
-
- namespace Xenta.Operations
- {
- /// <summary>
- /// Maps the account to role.
- /// </summary>
- public class MapAccountToRole : Operation<Boolean>
- {
- #region Ctors
-
- /// <summary>
- /// Initializes a new class instance.
- /// </summary>
- public MapAccountToRole()
- {
- AccountID = 0;
- RoleID = 0;
- }
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// Gets or sets the account identifier.
- /// </summary>
- public int AccountID
- {
- get;
- set;
- }
-
- /// <summary>
- /// Gets or sets the role identifier.
- /// </summary>
- public int RoleID
- {
- get;
- set;
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Executes the operation.
- /// </summary>
- /// <param name="uow">The unit of work.</param>
- public override void Execute(IUnitOfWork uow)
- {
- _result = false;
- var account = uow.Query<AccountEntity>()
- .Where(x => x.EntityID == AccountID)
- .FetchMany(x => x.Roles)
- .SingleOrDefault();
-
- if(account != null)
- {
- if(account.Roles.Any(x => x.EntityID == RoleID))
- {
- _result = true;
- }
- else
- {
- var role = uow.Execute(new GetRole
- {
- EntityID = RoleID
- }).Result;
-
- if(role != null)
- {
- account.Roles.Add(role);
- uow.Update(account);
-
- _result = true;
- }
- }
- }
- }
-
- #endregion
- }
-
- /// <summary>
- /// Unmaps the account from role.
- /// </summary>
- public class UnmapAccountFromRole : Operation
- {
- #region Ctors
-
- /// <summary>
- /// Initializes a new class instance.
- /// </summary>
- public UnmapAccountFromRole()
- {
- AccountID = 0;
- RoleID = 0;
- }
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// Gets or sets the account identifier.
- /// </summary>
- public int AccountID
- {
- get;
- set;
- }
-
- /// <summary>
- /// Gets or sets the role identifier.
- /// </summary>
- public int RoleID
- {
- get;
- set;
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Executes the operation.
- /// </summary>
- /// <param name="uow">The unit of work.</param>
- public override void Execute(IUnitOfWork uow)
- {
- var account = uow.Query<AccountEntity>()
- .Where(x => x.EntityID == AccountID)
- .FetchMany(x => x.Roles)
- .SingleOrDefault();
-
- if(account != null)
- {
- var role = account.Roles.SingleOrDefault(x => x.EntityID == RoleID);
-
- if(role != null)
- {
- account.Roles.Remove(role);
- uow.Update(account);
- }
- }
- }
-
- #endregion
- }
-
- /// <summary>
- /// Gets the account-role map.
- /// </summary>
- public class GetAccountRoleMap : Operation<IDictionary<String, Boolean>>,
- ISecuredOperation, IApiOperation, IDisclosable
- {
- #region Ctors
-
- /// <summary>
- /// Initializes a new class instance.
- /// </summary>
- public GetAccountRoleMap()
- : base(new Dictionary<String, Boolean>())
- {
- AccountID = 0;
- }
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// Gets the collection of security policies.
- /// </summary>
- public IEnumerable<SecurityPolicy> SecurityPolicies
- {
- get
- {
- yield return new SecurityPolicy()
- .IsAuthenticated()
- .HasPermission("ACCNT");
- }
- }
-
- /// <summary>
- /// Gets or sets the account identifier.
- /// </summary>
- public int AccountID
- {
- get;
- set;
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Executes the operation.
- /// </summary>
- /// <param name="uow">The unit of work.</param>
- public override void Execute(IUnitOfWork uow)
- {
- var query = uow.Query<RoleEntity>().Select(x => new
- {
- x.Code,
- Mapped = x.Accounts.Any(i => i.EntityID == AccountID)
- });
-
- foreach(var i in query.ToList())
- _result.Add(i.Code, i.Mapped);
- }
-
- /// <summary>
- /// Discloses the result.
- /// </summary>
- /// <param name="level">The disclosure level.</param>
- /// <returns>The result object.</returns>
- public object Disclose(DisclosureLevel level)
- {
- return _result;
- }
-
- #endregion
- }
-
- /// <summary>
- /// Maps the account to role.
- /// </summary>
- public class MapAccountToRoleByCode : Operation<Boolean>,
- ISecuredOperation, IApiOperation, IDisclosable
- {
- #region Ctors
-
- /// <summary>
- /// Initializes a new class instance.
- /// </summary>
- public MapAccountToRoleByCode()
- {
- AccountID = 0;
- RoleCode = String.Empty;
- }
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// Gets the collection of security policies.
- /// </summary>
- public IEnumerable<SecurityPolicy> SecurityPolicies
- {
- get
- {
- yield return new SecurityPolicy()
- .IsAuthenticated()
- .HasPermission("ACCNT");
- }
- }
-
- /// <summary>
- /// Gets or sets the account identifier.
- /// </summary>
- public int AccountID
- {
- get;
- set;
- }
-
- /// <summary>
- /// Gets or sets the role code.
- /// </summary>
- public string RoleCode
- {
- get;
- set;
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Executes the operation.
- /// </summary>
- /// <param name="uow">The unit of work.</param>
- public override void Execute(IUnitOfWork uow)
- {
- _result = false;
- var account = uow.Query<AccountEntity>()
- .Where(x => x.EntityID == AccountID)
- .FetchMany(x => x.Roles)
- .SingleOrDefault();
-
- if(account != null)
- {
- if(account.Roles.Any(x => x.Code.Equals(RoleCode)))
- {
- _result = true;
- }
- else
- {
- var role = uow.Execute(new GetRoleByCode
- {
- Code = RoleCode
- }).Result;
-
- if(role != null)
- {
- account.Roles.Add(role);
- uow.Update(account);
-
- _result = true;
- }
- }
- }
- }
-
- /// <summary>
- /// Discloses the result.
- /// </summary>
- /// <param name="level">The disclosure level.</param>
- /// <returns>The result object.</returns>
- public object Disclose(DisclosureLevel level)
- {
- return _result;
- }
-
- #endregion
- }
-
- /// <summary>
- /// Unmaps the account from role.
- /// </summary>
- public class UnmapAccountFromRoleByCode : Operation,
- ISecuredOperation, IApiOperation
- {
- #region Ctors
-
- /// <summary>
- /// Initializes a new class instance.
- /// </summary>
- public UnmapAccountFromRoleByCode()
- {
- AccountID = 0;
- RoleCode = String.Empty;
- }
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// Gets the collection of security policies.
- /// </summary>
- public IEnumerable<SecurityPolicy> SecurityPolicies
- {
- get
- {
- yield return new SecurityPolicy()
- .IsAuthenticated()
- .HasPermission("ACCNT");
- }
- }
-
- /// <summary>
- /// Gets or sets the account identifier.
- /// </summary>
- public int AccountID
- {
- get;
- set;
- }
-
- /// <summary>
- /// Gets or sets the role identifier.
- /// </summary>
- public string RoleCode
- {
- get;
- set;
- }
-
- #endregion
-
- #region Methods
-
- /// <summary>
- /// Executes the operation.
- /// </summary>
- /// <param name="uow">The unit of work.</param>
- public override void Execute(IUnitOfWork uow)
- {
- var account = uow.Query<AccountEntity>()
- .Where(x => x.EntityID == AccountID)
- .FetchMany(x => x.Roles)
- .SingleOrDefault();
-
- if(account != null)
- {
- var role = account.Roles.SingleOrDefault(x => x.Code.Equals(RoleCode));
-
- if(role != null)
- {
- account.Roles.Remove(role);
- uow.Update(account);
- }
- }
- }
-
- #endregion
- }
- }