예제 #1
0
        public override ConsoleResultModel Run()
        {
            ConsoleErrorResultModel errorResultModel;
            UserInfo userInfo;

            checkRoles();

            if ((errorResultModel = _userValidator.ValidateUser(UserId, PortalSettings, User, out userInfo)) != null)
            {
                return(errorResultModel);
            }

            try
            {
                _usersController.AddUserToRoles(User, userInfo.UserID, userInfo.PortalID, Roles, ",", StartDate, EndDate);
                int totalRoles;
                var userRoles = _usersController.GetUserRoles(userInfo, "", out totalRoles).Select(UserRoleModel.FromDnnUserRoleInfo).ToList();
                return(new ConsoleResultModel(string.Empty)
                {
                    Data = userRoles, Output = "Total Roles: " + totalRoles, Records = userRoles.Count
                });
            }
            catch (Exception ex)
            {
                return(new ConsoleErrorResultModel(ex.Message));
            }
        }
예제 #2
0
        public override ConsoleResultModel Run()
        {
            UserInfo userInfo;

            _userValidator.ValidateUser(UserId, PortalSettings, User, out userInfo);

            if (userInfo == null)
            {
                return(new ConsoleErrorResultModel(string.Format(LocalizeString("UserNotFound"), UserId)));
            }

            if (!userInfo.IsDeleted)
            {
                return(new ConsoleErrorResultModel(LocalizeString("Prompt_RestoreNotRequired")));
            }

            string message;
            var    restoredUser = _recyclebinController.RestoreUser(userInfo, out message);

            return(restoredUser
                ? new ConsoleResultModel(LocalizeString("UserRestored"))
            {
                Records = 1
            }
                : new ConsoleErrorResultModel(message));
        }
예제 #3
0
        public override ConsoleResultModel Run()
        {
            ConsoleErrorResultModel errorResultModel;
            UserInfo userInfo;

            if (
                (errorResultModel = _userValidator.ValidateUser(
                     UserId,
                     PortalSettings,
                     User,
                     out userInfo)
                ) != null
                )
            {
                return(errorResultModel);
            }

            //Don't allow self password change.
            if (userInfo.UserID == User.UserID)
            {
                return(new ConsoleErrorResultModel(LocalizeString("InSufficientPermissions")));
            }

            var success = UsersController.Instance.ForceChangePassword(userInfo, PortalId, Notify);

            return(success
                ? new ConsoleResultModel(LocalizeString("Prompt_PasswordReset") + (Notify ? LocalizeString("Prompt_EmailSent") : ""))
            {
                Records = 1
            }
                : new ConsoleErrorResultModel(LocalizeString("OptionUnavailable")));
        }
예제 #4
0
        public async Task <Domain.Models.Token> GenerateTokenAsync(string username, string password)
        {
            var userContext = await _userValidator.ValidateUser(username, password);

            if (string.IsNullOrEmpty(userContext.Id))
            {
                throw new InvalidCredentialException("Invalid username or password");
            }

            var expires = DateTime.Now.AddMinutes(5);

            var claims = _tokenClaims.Select(tokenClaim => tokenClaim.GenerateClaim(userContext)).ToList();

            var token = new JwtSecurityToken(new JwtHeader(new SigningCredentials(
                                                               new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ThisIsASecretKeyWhichNoOneCanHack")),
                                                               SecurityAlgorithms.HmacSha256)), new JwtPayload(claims));

            return(new Domain.Models.Token
            {
                AccessToken = new JwtSecurityTokenHandler().WriteToken(token),
                RefreshToken = "REFRESH",
                UserName = userContext.Name,
                Expiry = expires.ToString(CultureInfo.InvariantCulture)
            });
        }
예제 #5
0
        public async Task NotifyUsersAsync(User user, string content)
        {
            if (_userValidator.ValidateUser(user))
            {
                await _mailSender.SendAsync(user.Email, content);
            }

            throw new InvalidOperationException();
        }
예제 #6
0
        public override ConsoleResultModel Run()
        {
            ConsoleErrorResultModel errorResultModel;
            UserInfo userInfo;

            if ((errorResultModel = _userValidator.ValidateUser(UserId, PortalSettings, User, out userInfo)) != null)
            {
                return(errorResultModel);
            }

            var userModels = new List <UserModel> {
                new UserModel(userInfo)
            };

            if (userInfo.IsDeleted)
            {
                return(new ConsoleErrorResultModel(LocalizeString("Prompt_UserAlreadyDeleted")));
            }

            var validPortalId = userInfo.PortalID;

            if (!_userControllerWrapper.DeleteUserAndClearCache(ref userInfo, Notify, false))
            {
                return(new ConsoleErrorResultModel(LocalizeString("Prompt_UserDeletionFailed"))
                {
                    Data = userModels
                });
            }

            // attempt to retrieve the user from the dB
            userInfo   = _userControllerWrapper.GetUserById(validPortalId, userInfo.UserID);
            userModels = new List <UserModel> {
                new UserModel(userInfo)
            };
            return(new ConsoleResultModel(LocalizeString("UserDeleted"))
            {
                Data = userModels, Records = userModels.Count
            });
        }
예제 #7
0
 public static ConsoleErrorResultModel ValidateUser(int?userId, PortalSettings portalSettings, UserInfo currentUserInfo, out UserInfo userInfo)
 {
     return(_userValidator.ValidateUser(userId, portalSettings, currentUserInfo, out userInfo));
 }
예제 #8
0
        public override ConsoleResultModel Run()
        {
            var lst = new List <UserModel>();

            // if no argument, default to current user
            if (Args.Length == 1)
            {
                lst.Add(new UserModel(User));
            }
            else
            {
                var recCount = 0;
                var userId   = UserId;
                if (!userId.HasValue && !string.IsNullOrEmpty(Username))
                {
                    // do username lookup
                    var searchTerm = Username.Replace("%", "").Replace("*", "%");

                    userId = _userControllerWrapper.GetUsersByUserName(PortalId, searchTerm, -1, int.MaxValue, ref recCount, true, false) ?? UserIdZero;
                    // search against superusers if no regular user found
                    if (userId == UserIdZero)
                    {
                        //userId = (UserController.GetUsersByUserName(-1, searchTerm, -1, int.MaxValue, ref recCount, true, true).ToArray().FirstOrDefault() as UserInfo)?.UserID ?? UserIdZero;
                        userId = _userControllerWrapper.GetUsersByUserName(-1, searchTerm, -1, int.MaxValue, ref recCount, true, true) ?? UserIdZero;
                    }
                }
                else if (!userId.HasValue && !string.IsNullOrEmpty(Email))
                {
                    // must be email
                    var searchTerm = Email.Replace("%", "").Replace("*", "%");

                    userId = _userControllerWrapper.GetUsersByEmail(PortalId, searchTerm, -1, int.MaxValue, ref recCount, true, false) ?? UserIdZero;

                    // search against superusers if no regular user found
                    if (userId == UserIdZero)
                    {
                        userId = _userControllerWrapper.GetUsersByEmail(-1, searchTerm, -1, int.MaxValue, ref recCount, true, true) ?? UserIdZero;
                    }
                }

                UserInfo userInfo;
                ConsoleErrorResultModel errorResultModel =
                    _userValidator.ValidateUser(userId, PortalSettings, User, out userInfo);

                if (errorResultModel != null)
                {
                    return(errorResultModel);
                }

                lst.Add(new UserModel(userInfo));
            }

            return(new ConsoleResultModel(string.Empty)
            {
                Data = lst,
                Records = lst.Count,
                FieldOrder = new[]
                {
                    "UserId",
                    "Username",
                    "DisplayName",
                    "FirstName",
                    "LastName",
                    "Email",
                    "LastActivity",
                    "LastLogin",
                    "LastLockout",
                    "LastPasswordChange",
                    "IsDeleted",
                    "IsAuthorized",
                    "IsLockedOut",
                    "Created"
                }
            });
        }
예제 #9
0
        public override ConsoleResultModel Run()
        {
            var sbResults = new StringBuilder();

            ConsoleErrorResultModel errorResultModel;
            UserInfo userInfo;

            if (
                (errorResultModel = _userValidator.ValidateUser(
                     UserId,
                     PortalSettings,
                     User,
                     out userInfo)
                ) != null
                )
            {
                return(errorResultModel);
            }

            // Update the User
            // process the password first. If invalid, we can abort other changes to the user
            if (!string.IsNullOrEmpty(Password))
            {
                try
                {
                    _usersController.ChangePassword(userInfo.PortalID, userInfo.UserID, Password);
                    sbResults.Append(LocalizeString("ChangeSuccessful"));
                }
                catch (Exception ex)
                {
                    return(new ConsoleErrorResultModel(ex.Message));
                }
            }

            if (Approved.HasValue && userInfo.Membership.Approved != Approved.Value)
            {
                _usersController.UpdateAuthorizeStatus(userInfo, userInfo.PortalID, Approved.Value);
                sbResults.Append(LocalizeString(Approved.Value ? "UserAuthorized" : "UserUnAuthorized"));
            }

            var basicUpdated = !string.IsNullOrEmpty(Username) || !string.IsNullOrEmpty(DisplayName) || !string.IsNullOrEmpty(FirstName) || !string.IsNullOrEmpty(LastName) || !string.IsNullOrEmpty(Email);
            var userBasicDto = new UserBasicDto
            {
                Displayname = userInfo.DisplayName,
                UserId      = userInfo.UserID,
                Email       = userInfo.Email,
                IsDeleted   = userInfo.IsDeleted,
                Username    = userInfo.Username,
                Firstname   = userInfo.FirstName,
                Lastname    = userInfo.LastName
            };

            // Update Username
            if (!string.IsNullOrEmpty(Username))
            {
                userBasicDto.Username = Username;
            }
            // Update other properties
            if (!string.IsNullOrEmpty(DisplayName))
            {
                userBasicDto.Displayname = DisplayName;
            }
            if (!string.IsNullOrEmpty(FirstName))
            {
                userBasicDto.Firstname = FirstName;
            }
            if (!string.IsNullOrEmpty(LastName))
            {
                userBasicDto.Lastname = LastName;
            }
            if (!string.IsNullOrEmpty(Email))
            {
                userBasicDto.Email = Email;
            }
            if (basicUpdated)
            {
                try
                {
                    _usersController.UpdateUserBasicInfo(userBasicDto, userInfo.PortalID);
                }
                catch (SqlException)
                {
                    return(new ConsoleErrorResultModel(LocalizeString("UsernameNotUnique") + "\n" + sbResults));
                }
                catch (Exception ex)
                {
                    return(new ConsoleErrorResultModel(ex.Message + sbResults));
                }
            }
            // retrieve the updated user
            var updatedUser = _userControllerWrapper.GetUserById(userInfo.PortalID, userInfo.UserID);

            var lst = new List <UserModel> {
                new UserModel(updatedUser)
            };

            return(new ConsoleResultModel(string.Empty)
            {
                Data = lst,
                Records = lst.Count,
                FieldOrder = UserModel.FieldOrder,
                Output = LocalizeString("UserUpdated")
            });
        }