Пример #1
0
        /// <summary>
        /// Creates a role by a given name.
        /// </summary>
        /// <param name="roleName">The role to create.</param>
        /// <returns>Information about the user creation success</returns>
        public JsonResult CreateRole(string roleName)
        {
            MyJsonResult result;

            try
            {
                _accountService.CreateRole(roleName);
                result = MyJsonResult.CreateSuccess("The role " + roleName + " has been created.");
            }
            catch (Exception ex)
            {
                result = MyJsonResult.CreateError(ex);
            }

            return(Json(result));
        }
Пример #2
0
        /// <summary>
        /// Deletes a user from through the membership service.
        /// </summary>
        /// <param name="userId">The id of the user account which should be deleted.</param>
        /// <returns>Result info for the user account deletion action.</returns>
        public JsonResult DeleteUser(string username)
        {
            MyJsonResult result;

            try
            {
                _accountService.DeleteUser(username);
                result = MyJsonResult.CreateSuccess("The user " + username + " has been deleted.");
            }
            catch (Exception ex)
            {
                result = MyJsonResult.CreateError(ex);
            }

            return(Json(result));
        }
Пример #3
0
        /// <summary>
        /// Adds or removes a role for a user account.
        /// </summary>
        /// <param name="username">The user which roles should be modified.</param>
        /// <param name="rolename">The role which should be added or removed.</param>
        /// <param name="isInRole">The new role status for the user account. If false, the role will be deleted for the user account.</param>
        /// <returns></returns>
        public JsonResult AddRemoveRoleForUser(string username, string rolename, bool isInRole)
        {
            MyJsonResult result;

            try
            {
                _accountService.AddRemoveRoleForUser(username, rolename, isInRole);

                var action = isInRole ? "added" : "removed";
                var msg    = string.Format("The role {0} has been {1} for user {2}.", rolename, action, username);

                result = MyJsonResult.CreateSuccess(msg);
            }
            catch (ArgumentException ex)
            {
                result = MyJsonResult.CreateError("Could not remove role for user: " + ex.Message);
            }

            return(Json(result));
        }
Пример #4
0
        /// <summary>
        /// Creates a new user account
        /// </summary>
        /// <param name="username">A unique username</param>
        /// <param name="password">A hopefully secure password</param>
        /// <param name="email">A unique email address</param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public JsonResult CreateUser(string username, string password, string email)
        {
            MembershipCreateStatus status;
            MyJsonResult           result;

            //create user
            var user = _accountService.CreateUser(username, password, email, out status);

            if (status == MembershipCreateStatus.Success)
            {
                result      = MyJsonResult.CreateSuccess("The user account for " + username + " has been created.");
                result.data = CreateJsonUserObject(user);
            }
            else
            {
                result = MyJsonResult.CreateError(AccountValidation.ErrorCodeToString(status));
            }

            return(Json(result));
        }
Пример #5
0
        /// <summary>
        /// Unlocks the user by the specified username.
        /// </summary>
        /// <param name="userName">The user to unlock</param>
        /// <returns>Success or error information</returns>
        public JsonResult UnlockUser(string userName)
        {
            MyJsonResult result;

            try
            {
                if (_accountService.UnlockUser(userName))
                {
                    result = MyJsonResult.CreateSuccess("The account for " + userName + " has been unlocked");
                }
                else
                {
                    result = MyJsonResult.CreateError("Could not unlock the account for " + userName);
                }
            }
            catch (Exception ex)
            {
                result = MyJsonResult.CreateError(ex.Message);
            }

            return(Json(result));
        }
Пример #6
0
        /// <summary>
        /// Deletes a role by name.
        /// </summary>
        /// <param name="roleName">The role to delete.</param>
        /// <param name="allowPopulatedRoleDeletion">Allow role deletion even if users are still mapped to it.</param>
        /// <returns>Returns result information about the role deletion.</returns>
        public JsonResult DeleteRole(string roleName, bool allowPopulatedRoleDeletion)
        {
            MyJsonResult result;

            try
            {
                if (_accountService.DeleteRole(roleName, !allowPopulatedRoleDeletion))
                {
                    result = MyJsonResult.CreateSuccess("The role " + roleName + " has been deleted.");
                }
                else
                {
                    result = MyJsonResult.CreateError("The role " + roleName + " could not be deleted.");
                }
            }
            catch (Exception ex)
            {
                result = MyJsonResult.CreateError(ex.Message);
            }


            return(Json(result));
        }