Exemplo n.º 1
0
 /// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UsersInRole from, UsersInRole to)
 {
     if (to.IsPersisted)
     {
         if (from.IsSubPriorityModified && !to.IsSubPriorityModified)
         {
             to.SubPriority           = from.SubPriority;
             to.IsSubPriorityModified = true;
         }
         if (from.IsLastModifiedModified && !to.IsLastModifiedModified)
         {
             to.LastModified           = from.LastModified;
             to.IsLastModifiedModified = true;
         }
         if (from.IsAdminIDModified && !to.IsAdminIDModified)
         {
             to.AdminID           = from.AdminID;
             to.IsAdminIDModified = true;
         }
     }
     else
     {
         to.IsPersisted            = from.IsPersisted;
         to.RoleID                 = from.RoleID;
         to.UserID                 = from.UserID;
         to.AssignDate             = from.AssignDate;
         to.SubPriority            = from.SubPriority;
         to.IsSubPriorityModified  = from.IsSubPriorityModified;
         to.LastModified           = from.LastModified;
         to.IsLastModifiedModified = from.IsLastModifiedModified;
         to.AdminID                = from.AdminID;
         to.IsAdminIDModified      = from.IsAdminIDModified;
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) intrinsic identifiers.
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityTheSame(UsersInRole other)
 {
     if (other == null)
     {
         return(false);
     }
     else
     {
         return(RoleID == other.RoleID && UserID == other.UserID);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Internal use
        /// </summary>
        public UsersInRole ShallowCopy(bool allData = false, bool preserveState = false, bool checkLoadState = false)
        {
            UsersInRole e = new UsersInRole();

            e.StartAutoUpdating = false;
            e.RoleID            = RoleID;
            e.UserID            = UserID;
            e.AssignDate        = AssignDate;
            e.SubPriority       = SubPriority;
            if (preserveState)
            {
                e.IsSubPriorityModified = IsSubPriorityModified;
            }
            else
            {
                e.IsSubPriorityModified = false;
            }
            e.LastModified = LastModified;
            if (preserveState)
            {
                e.IsLastModifiedModified = IsLastModifiedModified;
            }
            else
            {
                e.IsLastModifiedModified = false;
            }
            e.AdminID = AdminID;
            if (preserveState)
            {
                e.IsAdminIDModified = IsAdminIDModified;
            }
            else
            {
                e.IsAdminIDModified = false;
            }
            e.DistinctString = GetDistinctString(true);
            e.IsPersisted    = IsPersisted;
            if (preserveState)
            {
                e.IsEntityChanged = IsEntityChanged;
            }
            else
            {
                e.IsEntityChanged = false;
            }
            e.StartAutoUpdating = true;
            return(e);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) primary key(s).
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityIdentical(UsersInRole other)
 {
     if (other == null)
     {
         return(false);
     }
     if (RoleID != other.RoleID)
     {
         return(false);
     }
     if (UserID != other.UserID)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Internal use
        /// </summary>
        public UsersInRole ShallowCopy(bool allData = false)
        {
            UsersInRole e = new UsersInRole();

            e.IsInitializing  = true;
            e.RoleID          = RoleID;
            e.UserID          = UserID;
            e.AssignDate      = AssignDate;
            e.SubPriority     = SubPriority;
            e.LastModified    = LastModified;
            e.AdminID         = AdminID;
            e.DistinctString  = GetDistinctString(true);
            e.IsPersisted     = true;
            e.IsEntityChanged = false;
            e.IsInitializing  = false;
            return(e);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
        /// </summary>
        /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
        /// <returns>
        /// </returns>
        public void UpdateChanges(UsersInRole newdata)
        {
            int cnt = 0;

            if (SubPriority != newdata.SubPriority)
            {
                SubPriority           = newdata.SubPriority;
                IsSubPriorityModified = true;
                cnt++;
            }
            if (LastModified != newdata.LastModified)
            {
                LastModified           = newdata.LastModified;
                IsLastModifiedModified = true;
                cnt++;
            }
            IsEntityChanged = cnt > 0;
        }
 public static async Task<OperationResult> AddUserToRole(string adminId, string uid, int rid)
 {
     OperationResult OpResult = new OperationResult();
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     RoleServiceProxy rsvc = new RoleServiceProxy();
     UserServiceProxy usvc = new UserServiceProxy();
     var cntx = Cntx;
     var u = await usvc.LoadEntityByKeyAsync(cntx, uid);
     if (u == null)
     {
         OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
         return OpResult;
     }
     var uroles = await usvc.MaterializeAllRolesAsync(cntx, u);
     if (DBAutoCleanupRoles)
     {
         // prevent polution
         List<Role> higherroles = new List<Role>();
         foreach (var ur in uroles)
         {
             var pr = ur;
             if (pr.ID == rid)
                 higherroles.Add(ur);
             while (pr.ParentID != null)
             {
                 pr.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, pr);
                 pr = pr.UpperRef;
                 if (pr.ID == rid)
                 {
                     higherroles.Add(ur);
                     break;
                 }
             }
         }
         if (higherroles.Count > 0)
         {
             string rolesstr = "";
             foreach (var hr in higherroles)
                 rolesstr += (rolesstr == "" ? "" : ", ") + hr.DistinctString;
             string errorfmt = ResourceUtils.GetString("43558b5deaec392b9461d28d4e753687", "Operation denied: the user already has this or more specific roles: '{0}'! Try to remove them before adding present one.");
             OpResult.Result = new { ok = false, msg = string.Format(errorfmt, rolesstr) };
             return OpResult;
         }
     }
     var r = await rsvc.LoadEntityByKeyAsync(cntx, rid);
     if (r == null)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("db2a3d7bc44d36a9ebeaa0d562c4cd21", "The role is not found.") };
         return OpResult;
     }
     else if (r.RolePriority > maxp.Major)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("67729f0f407d1ea57f28b43235b3e5f6", "Adding more priviledged role is not authorized.") };
         return OpResult;
     }
     List<SimpleMessage> notices = new List<SimpleMessage>();
     var uir = new UsersInRole();
     List<Role> removed = new List<Role>();
     if (DBAutoCleanupRoles)
     {
         // clean up: find more general roles to remove.
         var p = r;
         while (p.ParentID != null)
         {
             p.UpperRef = await rsvc.MaterializeUpperRefAsync(cntx, p);
             p = p.UpperRef;
             foreach (var ur in uroles)
             {
                 if (ur.ID == p.ID)
                 {
                     if (!(from d in removed where d.ID == p.ID select d).Any())
                         removed.Add(p);
                 }
             }
         }
     }
     uir.IsPersisted = false;
     uir.UserID = u.ID;
     uir.RoleID = rid;
     uir.SubPriority = 0;
     uir.AssignDate = DateTime.UtcNow;
     uir.LastModified = uir.AssignDate;
     uir.AdminID = adminId;
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);
     notices.Add(new SimpleMessage
                 {
                     TypeId = 1,
                     Title = string.Format(ResourceUtils.GetString("38015f8af3e032dfd803758dd2bde917", "New role: [{0}] is added.", memb.AcceptLanguages), r.DistinctString),
                     Data = "{ id=\"" + r.ID + "\", type=\"role\", name=\"" + r.DistinctString + "\" }"
                 });
     var _r = new { id = rid, uid = u.ID, name = r.RoleName, path = r.DistinctString, level = uir.SubPriority, op = true };
     List<dynamic> _removed = new List<dynamic>();
     if (removed.Count > 0)
     {
         List<UsersInRole> l = new List<UsersInRole>();
         foreach (var rmv in removed)
         {
             var x = uirsvc.LoadEntityByKey(Cntx, rmv.ID, u.ID);
             l.Add(x);
             _removed.Add(new { id = rmv.ID, name = rmv.RoleName, path = rmv.DistinctString, op = maxp.Major >= rmv.RolePriority });
         }
         await uirsvc.DeleteEntitiesAsync(Cntx, new UsersInRoleSet(), l.ToArray());
         foreach (var _rrmv in removed)
             notices.Add(new SimpleMessage
             {
                 TypeId = 1,
                 Title = string.Format(ResourceUtils.GetString("9708d527fbbf0d9752fc2c741615fb58", "Your role: [{0}] is removed.", memb.AcceptLanguages), _rrmv.DistinctString),
                 Data = "{ id=\"" + _rrmv.ID + "\", type=\"role\", name=\"" + _rrmv.DistinctString + "\" }"
             });
     }
     await AddUserRoleHistory(uir, UserRoleOperations.Added);
     OpResult.Result = new { ok = true, msg = "", added = _r, removed = _removed.ToArray() };
     OpResult.notices = notices.ToArray();
     return OpResult;
 }
 private static async Task AddUserRoleHistory(UsersInRole current, UserRoleOperations op)
 {
     UsersRoleHistoryServiceProxy hrhsvc = new UsersRoleHistoryServiceProxy();
     UsersRoleHistorySet urhs = new UsersRoleHistorySet();
     UsersRoleHistory urh = new UsersRoleHistory();
     urh.UserID = current.UserID;
     urh.RoleID = current.RoleID;
     urh.SubPriority = current.SubPriority;
     urh.OperatorID = current.AdminID;
     urh.ChangeDate = DateTime.UtcNow;
     switch (op)
     {
         case UserRoleOperations.Added:
             urh.Operation = urhs.OperationValues[0];
             break;
         case UserRoleOperations.Modified:
             urh.Operation = urhs.OperationValues[1];
             break;
         case UserRoleOperations.Deleted:
             urh.Operation = urhs.OperationValues[2];
             break;
     }
     await hrhsvc.AddOrUpdateEntitiesAsync(Cntx, urhs, new UsersRoleHistory[] { urh });
 }
Exemplo n.º 9
0
 /// <summary>
 /// Internal use
 /// </summary>
 public UsersInRole ShallowCopy(bool allData = false)
 {
     UsersInRole e = new UsersInRole();
     e.IsInitializing = true;
     e.RoleID = RoleID;
     e.UserID = UserID;
     e.AssignDate = AssignDate;
     e.SubPriority = SubPriority;
     e.LastModified = LastModified;
     e.AdminID = AdminID;
     e.DistinctString = GetDistinctString(true);
     e.IsPersisted = true;
     e.IsEntityChanged = false;
     e.IsInitializing = false;
     return e;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
 /// </summary>
 /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
 /// <returns>
 /// </returns>
 public void UpdateChanges(UsersInRole newdata)
 {
     int cnt = 0;
     if (SubPriority != newdata.SubPriority)
     {
         SubPriority = newdata.SubPriority;
         IsSubPriorityModified = true;
         cnt++;
     }
     if (LastModified != newdata.LastModified)
     {
         LastModified = newdata.LastModified;
         IsLastModifiedModified = true;
         cnt++;
     }
     IsEntityChanged = cnt > 0;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UsersInRole from, UsersInRole to)
 {
     if (to.IsPersisted)
     {
         if (from.IsSubPriorityModified && !to.IsSubPriorityModified)
         {
             to.SubPriority = from.SubPriority;
             to.IsSubPriorityModified = true;
         }
         if (from.IsLastModifiedModified && !to.IsLastModifiedModified)
         {
             to.LastModified = from.LastModified;
             to.IsLastModifiedModified = true;
         }
         if (from.IsAdminIDModified && !to.IsAdminIDModified)
         {
             to.AdminID = from.AdminID;
             to.IsAdminIDModified = true;
         }
     }
     else
     {
         to.IsPersisted = from.IsPersisted;
         to.RoleID = from.RoleID;
         to.UserID = from.UserID;
         to.AssignDate = from.AssignDate;
         to.SubPriority = from.SubPriority;
         to.IsSubPriorityModified = from.IsSubPriorityModified;
         to.LastModified = from.LastModified;
         to.IsLastModifiedModified = from.IsLastModifiedModified;
         to.AdminID = from.AdminID;
         to.IsAdminIDModified = from.IsAdminIDModified;
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) intrinsic identifiers.
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityTheSame(UsersInRole other)
 {
     if (other == null)
         return false;
     else
         return RoleID == other.RoleID &&  UserID == other.UserID;
 }              
Exemplo n.º 13
0
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) primary key(s).
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityIdentical(UsersInRole other)
 {
     if (other == null)
         return false;
     if (RoleID != other.RoleID)
         return false;
     if (UserID != other.UserID)
         return false;
     return true;
 }              
Exemplo n.º 14
0
 /// <summary>
 /// Internal use
 /// </summary>
 public UsersInRole ShallowCopy(bool allData = false, bool preserveState = false)
 {
     UsersInRole e = new UsersInRole();
     e.StartAutoUpdating = false;
     e.RoleID = RoleID;
     e.UserID = UserID;
     e.AssignDate = AssignDate;
     e.SubPriority = SubPriority;
     if (preserveState)
         e.IsSubPriorityModified = IsSubPriorityModified;
     else
         e.IsSubPriorityModified = false;
     e.LastModified = LastModified;
     if (preserveState)
         e.IsLastModifiedModified = IsLastModifiedModified;
     else
         e.IsLastModifiedModified = false;
     e.AdminID = AdminID;
     if (preserveState)
         e.IsAdminIDModified = IsAdminIDModified;
     else
         e.IsAdminIDModified = false;
     e.DistinctString = GetDistinctString(true);
     e.IsPersisted = IsPersisted;
     if (preserveState)
         e.IsEntityChanged = IsEntityChanged;
     else
         e.IsEntityChanged = false;
     e.StartAutoUpdating = true;
     return e;
 }