Ejemplo n.º 1
0
        public bool IsUserInRole(string username, string roleName)
        {
            ShopgunMembershipUser user = _membershipProviderApplicationService.GetUser(username, false, "ShopgunMembershipProvider") as ShopgunMembershipUser;

            Role role = _roleRepository.FindOne(r => r.RoleName == roleName);

            var userInRoleResult = _usersInRoleRepository.Find(u => (u.Role == role) && (u.User.Id == user.UserId));

            return(userInRoleResult.Count() > 0);
        }
        public ShopgunMembershipWebserviceGateway CreateUser(string username, string password, string email)
        {
            ShopgunMembershipWebserviceGateway response = new ShopgunMembershipWebserviceGateway();

            User newUser = new User
            {
                UserName                = username,
                Password                = password,
                Email                   = email,
                CreationDate            = DateTime.Now,
                LastActivity            = DateTime.Now,
                LastLockedOutDate       = DateTime.Now,
                LastLoginDate           = DateTime.Now,
                LastPasswordChangedDate = DateTime.Now
            };

            //TODO: Shall support language, translate from resource string.

            //Refactoring here please, extract to a method: ValidateNewUserInfo(string username, string password, string email) : bool
            if ((newUser.UserName == null) || (newUser.Password == null))
            {
                response.value   = false;
                response.message = "Enter username and password!";
                return(response);
            }
            if (_membershipProviderApplicationService.GetUser(newUser.UserName, false, ProviderName) != null)
            {
                response.value   = false;
                response.message = "Username already exists!";
                return(response);
            }
            if (_membershipProviderApplicationService.GetUserByMail(newUser.Email, ProviderName) != null)
            {
                response.value   = false;
                response.message = "Email already exists!";
                return(response);
            }
            //End of refactoring

            try
            {
                _membershipProviderApplicationService.CreateUser(newUser);
                response = ValidateMobileUser(newUser.UserName, password);
                if (response.value)
                {
                    response.message = "User created!";
                }
                return(response);
            }
            catch
            {
                response.value   = false;
                response.message = "User not created, system error!";
                return(response);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a new membership user to the data source.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="passwordQuestion"></param>
        /// <param name="passwordAnswer"></param>
        /// <param name="isApproved"></param>
        /// <param name="providerUserKey"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            //TODO: All code shall be moved into the application service.
            User newUser = new User();

            newUser.UserName                = username;
            newUser.Password                = password;
            newUser.Email                   = email;
            newUser.CreationDate            = DateTime.Now;
            newUser.LastActivity            = DateTime.Now;
            newUser.LastLockedOutDate       = DateTime.Now;
            newUser.LastLoginDate           = DateTime.Now;
            newUser.LastPasswordChangedDate = DateTime.Now;


            if (_membershipProviderApplicationService.GetUser(newUser.UserName, false, ProviderName) != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
            }
            else if (_membershipProviderApplicationService.GetUserByMail(newUser.Email, ProviderName) != null)
            {
                status = MembershipCreateStatus.DuplicateEmail;
            }
            else
            {
                try
                {
                    _membershipProviderApplicationService.CreateUser(newUser);
                    status = MembershipCreateStatus.Success;
                    return(newUser.ToMembershipUser());
                }
                catch (Exception e)
                {
                    status = MembershipCreateStatus.UserRejected;
                    throw new Exception(e.Message, e);
                }
            }


            return(null);
        }