public SystemRole CreateSystemRole(string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments)
        {
            if(string.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("Missing role name");
            }

            permissions = permissions ?? new SystemRolePermissions();
            assignments = assignments ?? new SystemRoleAssignments();

            if(_documentSession.QueryNoCache<SystemRole>().Any(i=>i.RoleName == roleName))
            {
                throw new ArgumentException(string.Format("Role with role name {0} already exists", roleName));
            }
            string systemRoleId = Guid.NewGuid().ToString();
            var role = new SystemRole
            {
                Id = systemRoleId,
                RoleName = roleName,
                Assignments = assignments,
                Permissions = permissions,  
                RoleType = roleType
            };
            role.SetCreatedFields(_userIdentity.UserName);
            
            return _documentSession.StoreSaveEvict(role);
        }
 public SystemRole UpdateSystemRole(string systemRoleId, string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments)
 {
     if(string.IsNullOrEmpty(roleName))
     {
         throw new ArgumentNullException("Missing role name");
     }
     var existingItem = _documentSession.QueryNoCacheNotStale<SystemRole>().Any(i=>i.Id != systemRoleId && i.RoleName == roleName);
     if(existingItem)
     {
         throw new ArgumentException("RoleName already exists: " + roleName);
     }
     var role = _documentSession.LoadEnsure<SystemRole>(systemRoleId);
     role.RoleName = roleName;
     role.RoleType = roleType;
     role.SetUpdatedFields(_userIdentity.UserName);
     role.Permissions = permissions ?? new SystemRolePermissions();
     role.Assignments = assignments ?? new SystemRoleAssignments();
     return _documentSession.SaveEvict(role);
 }
        public SystemRole UpdateSystemRole(string systemRoleId, string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments)
        {
            if(string.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }
            using(var db = _sqlConnectionInfo.GetDB())
            {
                var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM SystemRole WHERE RoleName=@0 AND ID<>@1", roleName, systemRoleId);
                if(count != 0)
                {
                    throw new ArgumentException("RoleName already exists: " + roleName);
                }
            }
            var item = this.GetDBItem(systemRoleId);
            item.RoleName = roleName;
            item.RoleType = roleType;
            item.PermissionsJson = (permissions ?? new SystemRolePermissions()).ToJson();
            item.SetUpdatedFields(_userIdentity.UserName);
            using(var db = _sqlConnectionInfo.GetDB())
            {
                db.Update("SystemRole", "ID", item, item.Id);

                if(assignments == null || assignments.UserNameList == null || assignments.UserNameList.Count == 0)
                {
                    db.Execute("DELETE FROM SystemRoleUser WHERE SystemRoleID=@0", systemRoleId);
                }
                else 
                {
                    var itemsToAdd = new List<string>();
                    var itemsToDelete = new List<SqlSystemRoleUser>();

                    foreach(var newUserName in assignments.UserNameList)
                    {
                        if(!item.SystemRoleUserList.Any(i=>i.UserName.Equals(newUserName, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            itemsToAdd.Add(newUserName);
                        }
                    }
                    foreach(var oldUser in item.SystemRoleUserList)
                    {
                        if(!assignments.UserNameList.Contains(oldUser.UserName, StringComparer.CurrentCultureIgnoreCase))
                        {
                            itemsToDelete.Add(oldUser);
                        }
                    }
                    foreach(var newUserName in itemsToAdd)
                    {
                        var dbItem = new SqlSystemRoleUser
                        {
                            Id = Guid.NewGuid().ToString(),
                            SystemRoleId = systemRoleId, 
                            UserName = newUserName
                        };
                        db.Insert("SystemRoleUser", "ID", false, dbItem);
                    }
                    foreach(var oldUser in itemsToDelete)
                    {
                        db.Execute("DELETE FROM SystemRoleUser WHERE ID=@0", oldUser.Id);
                    }
                }
            }
            return this.GetSystemRole(item.Id);
        }
 public SystemRole CreateSystemRole(string roleName, EnumSystemRoleType roleType, SystemRolePermissions permissions, SystemRoleAssignments assignments)
 {
     if(string.IsNullOrEmpty(roleName))
     {
         throw new ArgumentNullException("roleName");
     }
     using (var db = _sqlConnectionInfo.GetDB())
     {
         var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM SystemRole WHERE RoleName=@0", roleName);
         if (count != 0)
         {
             throw new ArgumentException("RoleName already exists: " + roleName);
         }
     }
     var item = new SqlSystemRole
     {
         Id = Guid.NewGuid().ToString(),
         RoleName = roleName, 
         RoleType = roleType,
         PermissionsJson = (permissions ?? new SystemRolePermissions()).ToJson(),
         //AssignmentsJson = (assignments ?? new SystemRoleAssignments()).ToJson()
     };
     item.SetCreatedFields(_userIdentity.UserName);
     using(var db = _sqlConnectionInfo.GetDB())
     {
         db.Insert("SystemRole", "ID", false, item);
         
         if(assignments != null && assignments.UserNameList != null)
         {
             foreach(var userName in assignments.UserNameList)
             {
                 var userItem = new SqlSystemRoleUser
                 {
                     Id = Guid.NewGuid().ToString(),
                     SystemRoleId = item.Id,
                     UserName = userName
                 };
                 db.Insert("SystemRoleUser", "ID", false, userItem);
             }
         }
     }
     return this.GetSystemRole(item.Id);
 }
 public SystemRole UpdateSystemRole(string systemRoleId, string roleName, SystemRolePermissions permissions, SystemRoleAssignments assignments)
 {
     var role = _systemRoleRepository.GetSystemRole(systemRoleId);
     return _systemRoleRepository.UpdateSystemRole(systemRoleId, roleName, role.RoleType, permissions, assignments);
 }
 public SystemRole CreateSystemRole(string roleName, SystemRolePermissions permissions, SystemRoleAssignments assignments)
 {
     return _systemRoleRepository.CreateSystemRole(roleName, EnumSystemRoleType.Normal, permissions, assignments);
 }
Exemplo n.º 7
0
 public SystemRole()
 {
     this.Permissions = new SystemRolePermissions();
     this.Assignments = new SystemRoleAssignments();
 }