Esempio n. 1
0
        public int CreateRole(Role role)
        {
            SecRole secRole = new SecRole()
            {
                RoleCode  = role.RoleCode,
                RoleLabel = role.RoleLabel,
                CreatedAt = DateTime.Now
            };

            foreach (Permission permission in role.Permissions)
            {
                SecRolePermission secRolePermission = new SecRolePermission();
                secRolePermission.PermissionId = permission.PermissionId;
                secRole.SecRolePermission.Add(secRolePermission);
            }
            return(_repository.CreateRole(secRole));
        }
Esempio n. 2
0
 public int CreateSecRolePermission(SecRolePermission secRolePermission)
 {
     context.SecRolePermission.Add(secRolePermission);
     context.SaveChanges();
     return(secRolePermission.RolePermissionId);
 }
Esempio n. 3
0
        public int UpdateRole(Role uiRole)
        {
            SecRole oldRole = _repository.RetrieveSecRole(uiRole.RoleId);

            SecRole updatedRole = new SecRole
            {
                RoleId    = oldRole.RoleId,
                RoleCode  = oldRole.RoleCode,
                RoleLabel = uiRole.RoleLabel,
                UpdatedAt = DateTime.Now
            };

            HashSet <int> oldPermissionIdSet = new HashSet <int>(oldRole.SecRolePermission.Select(p => p.PermissionId));
            HashSet <int> newPermissionIdSet = new HashSet <int>(uiRole.Permissions.Select(p => p.PermissionId));

            HashSet <int> redundantPermissionIdSet  = new HashSet <int>();
            HashSet <int> softDeletePermissionIdSet = new HashSet <int>();
            HashSet <int> addToPermissionIdSet      = new HashSet <int>();

            foreach (int oldPermissionId in oldPermissionIdSet)
            {
                if (newPermissionIdSet.Contains(oldPermissionId))
                {
                    // If the old permission is in the new permission set,
                    // mark it as redundant because this sec_role_permission
                    // row is already in the database.
                    redundantPermissionIdSet.Add(oldPermissionId);
                }
                else
                {
                    // If the old permission set is not in the new permission set,
                    // mark it for soft deletion
                    softDeletePermissionIdSet.Add(oldPermissionId);
                }
            }

            foreach (int newPermissionId in newPermissionIdSet)
            {
                // If the new permission is not in the old permission set,
                // mark it for addition.
                if (oldPermissionIdSet.Contains(newPermissionId) == false)
                {
                    addToPermissionIdSet.Add(newPermissionId);
                }
            }

            ///////////////////////////////////////////////////////////////////////////////
            // Redundant or Reactivated Permissions
            ///////////////////////////////////////////////////////////////////////////////
            foreach (int redundantPermissionId in redundantPermissionIdSet)
            {
                foreach (SecRolePermission oldSecRolePerm in oldRole.SecRolePermission)
                {
                    if (oldSecRolePerm.PermissionId == redundantPermissionId)
                    {
                        if (oldSecRolePerm.DeletedAt == null)
                        {
                            //  If DeletedAt IS NULL, do not add this SecRolePermission to the newly
                            //  created role because there haven't been any changes.
                        }
                        else
                        {
                            // If DeletedAt IS NOT NULL, this SecRolePermission needs to be
                            // reactivated by setting DeletedAt to NULL and UpdatedAt to DateTime.Now
                            SecRolePermission reactivatedSecRolePerm = new SecRolePermission
                            {
                                RolePermissionId = oldSecRolePerm.RolePermissionId,
                                RoleId           = oldSecRolePerm.RoleId,
                                PermissionId     = oldSecRolePerm.PermissionId,
                                CreatedAt        = oldSecRolePerm.CreatedAt,
                                DeletedAt        = null,
                                UpdatedAt        = DateTime.Now
                            };

                            updatedRole.SecRolePermission.Add(reactivatedSecRolePerm);
                        }
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////////////////
            // SOFT DELETES
            // -- SecRolePermission in old SecRole but not in new UIModel.Row
            // -- set DeletedAt to DateTime.Now
            ///////////////////////////////////////////////////////////////////////////////
            foreach (int softDeletePermissionId in softDeletePermissionIdSet)
            {
                foreach (SecRolePermission oldSecRolePerm in oldRole.SecRolePermission)
                {
                    if (oldSecRolePerm.PermissionId == softDeletePermissionId)
                    {
                        SecRolePermission reactivatedSecRolePerm = new SecRolePermission
                        {
                            RolePermissionId = oldSecRolePerm.RolePermissionId,
                            RoleId           = oldSecRolePerm.RoleId,
                            PermissionId     = oldSecRolePerm.PermissionId,
                            CreatedAt        = oldSecRolePerm.CreatedAt,
                            DeletedAt        = DateTime.Now,
                            UpdatedAt        = oldSecRolePerm.UpdatedAt
                        };
                        updatedRole.SecRolePermission.Add(reactivatedSecRolePerm);
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////////////////
            // NEW PERMISSIONS
            // UIModel.RolePermission in new UIModel.Role but not in old SecRole
            // Add to SecRole's SecRolePermission collection before saving new version
            // of SecRole to database.
            ///////////////////////////////////////////////////////////////////////////////
            foreach (int addToPermissionId in addToPermissionIdSet)
            {
                SecRolePermission newSecRolePerm = new SecRolePermission
                {
                    RoleId       = updatedRole.RoleId,
                    PermissionId = addToPermissionId
                };

                updatedRole.SecRolePermission.Add(newSecRolePerm);
            }

            return(_repository.UpdateRole(updatedRole));
        }