Beispiel #1
0
        public override void Process(UserViewModel viewModel)
        {
            var currentUserPermissions = permissionProvider
                                         .GetPermissions(UserId)
                                         .Select(x => x.Action)
                                         .ToList();
            var user  = userProvider.GetEntity(x => x.Id == viewModel.Id);
            var roles = roleProvider.GetAllAsList();

            using (var transaction = new TransactionScope(TransactionScopeOption.Required))
            {
                if (user.IsNullOrDefault())
                {
                    if (!currentUserPermissions.Contains(Permissions.GetActionKey(Module.Dashboard, Type.User, Action.Edit)))
                    {
                        return;
                    }

                    user = new User
                    {
                        Email          = viewModel.Email,
                        Password       = string.Empty,
                        Firstname      = viewModel.Firstname,
                        Lastname       = viewModel.Lastname,
                        ResetToken     = string.Empty,
                        ResetTokenDate = null,
                        Active         = viewModel.Active
                    };

                    userService.Add(user);
                }
                else
                {
                    user.Firstname = viewModel.Firstname;
                    user.Lastname  = viewModel.Lastname;

                    if (currentUserPermissions.Contains(Permissions.GetActionKey(Module.Dashboard, Type.User, Action.Edit)))
                    {
                        user.Email  = viewModel.Email;
                        user.Active = viewModel.Active;
                    }

                    userService.AddOrUpdate(user);
                    var currentUserRoles = userRoleProvider.Where(x => x.User_Id == user.Id);
                    userRoleService.DeleteRange(currentUserRoles);
                }

                var selectedRoles = viewModel.Roles.Where(x => x.Active);
                var userRoles     = roles.Where(x => selectedRoles.Any(y => y.Id == x.Id))
                                    .Select(role => new UserRole
                {
                    User_Id = user.Id,
                    User    = user,
                    Role_Id = role.Id,
                    Role    = role
                }).ToList();

                userRoleService.AddRange(userRoles);

                transaction.Complete();
            }
        }