예제 #1
0
 internal static UserDto GetUser(User user)
 {
     return new UserDto
     {
         ID = user.ID,
         Name = user.Name,
         Roles = user.RoleAssignments == null ? null : string.Join(",", user.RoleAssignments.Select(p => p.Role.Name).OrderBy(p => p)),
         RoleIds = user.RoleAssignments == null ? null : user.RoleAssignments.Select(p => p.RoleID),
     };
 }
예제 #2
0
        internal static void SaveUser(ViewModels.UserEditVM user)
        {
            int id = user.ID;
            var userUpdate = new User
            {
                ID = user.ID,
                Name = user.Name,
            };
            if (id == 0)
            {
                Db.Users.Add(userUpdate);
                Db.SaveChanges();
                id = userUpdate.ID;
            }
            else
            {
                userUpdate = Db.Users.FirstOrDefault(p => p.ID == id);
                userUpdate.Name = user.Name;
                foreach (var assignment in Db.RoleAssignments.Where(p => p.UserID == id))
                {
                    userUpdate.RoleAssignments.Remove(assignment);
                    Db.RoleAssignments.Remove(assignment);
                }

                Db.Users.AddOrUpdate(userUpdate);
                Db.SaveChanges();
            }

            foreach (var roleid in user.SelectedRoles)
            {
                Db.RoleAssignments.Add(new RoleAssignment
                {
                    UserID = id,
                    RoleID = roleid
                });
            }
            Db.SaveChanges();
        }