public override IList <PersonType> GetPersonTypesByUser(string userName)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                // Find all the Persons who match the MembershipUser id of 'username'.
                var  userDS = new UserDataStore(transaction);
                User user   = userDS.FindByName(_applicationName, userName);

                // If there is no matching person, then do not continue.
                if (user.Person == null)
                {
                    throw new PersonNotFoundException(user.Name);
                }

                // Then, find all the PersonTypes via PersonPersonType that match the personIds.
                IList <PersonType> runningList = new List <PersonType>();
                var ppDS = new PersonPersonTypeDataStore(transaction);
                IList <PersonPersonType> pps = ppDS.FindPersonTypesByPerson(user.Person.Id);
                foreach (PersonPersonType ppt in pps)
                {
                    IEnumerable <PersonType> q = from x in runningList where x.Id.Equals(ppt.PersonType.Id) select x;
                    if (q.Count() == 0)
                    {
                        runningList.Add(ppt.PersonType);
                    }
                }
                return(runningList);
            }
        }
Beispiel #2
0
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var usersInRolesStore = new UserInRoleDataStore(transaction);

                foreach (string userName in usernames)
                {
                    foreach (string roleName in roleNames)
                    {
                        var        rDS        = new RoleDataStore(transaction);
                        Role       role       = rDS.FindByName(mApplicationName, roleName);
                        var        uDS        = new UserDataStore(transaction);
                        User       user       = uDS.FindByName(mApplicationName, userName);
                        UserInRole userInRole = usersInRolesStore.Find(ApplicationName, user, role);
                        if (userInRole == null)
                        {
                            throw new UserInRoleNotFoundException(userName, roleName);
                        }

                        userInRole.Deleted = true;
                        usersInRolesStore.Update(userInRole);
                    }
                }

                transaction.Commit();
            }
        }
        public override bool IsUserInPersonType(string userName, string personTypeId)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var pptDS = new PersonPersonTypeDataStore(transaction);
                IList <PersonPersonType> peoples = pptDS.FindPersonByPersonType(personTypeId);

                // Find the person who corresponds to username.
                var  uDS  = new UserDataStore(transaction);
                User user = uDS.FindByName(_applicationName, userName);

                // If there is no matching person, then do not continue.
                if (user.Person == null)
                {
                    throw new PersonNotFoundException(user.Name);
                }

                IEnumerable <PersonPersonType> q = from x in peoples where x.Person.Id.Equals(user.Person.Id) select x;

                if (q.Count() > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="isAssigned">A site can be not assigned to a user, but still exists as a record,
        /// this flag can suppress those that are not assigned.</param>
        /// <returns></returns>
        public override IList <Site> GetSitesByUser(string userName, bool isAssigned)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                // Find all the Persons who match the MembershipUser id of 'username'.
                var  userDs = new UserDataStore(transaction);
                User user   = userDs.FindByName(_applicationName, userName);

                // Then, find all the sites via PersonSite that match the personIds.
                IList <Site> runningList = new List <Site>();
                var          ppDs        = new PersonSiteDataStore(transaction);
                if (user.Person != null)
                {
                    IList <PersonSite> pps = ppDs.FindSitesByPerson(user.Person.Id, isAssigned);
                    foreach (PersonSite pSite in pps)
                    {
                        IEnumerable <Site> q = from x in runningList where x.Id.Equals(pSite.Site.Id) select x;
                        if (!q.Any())
                        {
                            runningList.Add(pSite.Site);
                        }
                    }
                    return(runningList);
                }
            }
            return(new List <Site>());
        }
        public override Person GetPersonByUserName(string userName)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var  uDS  = new UserDataStore(transaction);
                User user = uDS.FindByName(_applicationName, userName);

                if (user != null)
                {
                    return(user.Person);
                }
            }
            return(null);
        }
Beispiel #6
0
        public override bool IsUserInRole(string username, string roleName)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var  rDS  = new RoleDataStore(transaction);
                Role role = rDS.FindByName(mApplicationName, roleName);
                var  uDS  = new UserDataStore(transaction);
                User user = uDS.FindByName(mApplicationName, username);
                var  userInRoleDataStore = new UserInRoleDataStore(transaction);

                if (userInRoleDataStore.Find(ApplicationName, user, role) != null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #7
0
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var userNames = new List <string>();

            using (var transaction = new TransactionScope(mConfiguration))
            {
                var  rDS  = new RoleDataStore(transaction);
                Role role = rDS.FindByName(mApplicationName, roleName);
                var  uDS  = new UserDataStore(transaction);
                User user = uDS.FindByName(mApplicationName, usernameToMatch);
                var  userInRoleDataStore = new UserInRoleDataStore(transaction);

                IList <UserInRole> listUserInRole = userInRoleDataStore.FindForUserAndRole(ApplicationName, user, role);

                foreach (UserInRole ur in listUserInRole)
                {
                    userNames.Add(ur.User.Name);
                }
            }

            return(userNames.ToArray());
        }
Beispiel #8
0
        public override IList <User> GetAccessibleItemUsers(Access access)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                if (access.AllUsers)
                {
                    var userStore = new UserDataStore(transaction);
                    return(userStore.FindAll(_applicationName));
                }

                if (access.AllPersonTypes && access.AllSites)
                {
                    var userStore = new UserDataStore(transaction);
                    return(userStore.FindAll(_applicationName));
                }

                if (access.UserId != null)
                {
                    var userStore      = new UserDataStore(transaction);
                    var singleUserList = new List <User>();
                    singleUserList.Add(userStore.FindByName(_applicationName, access.UserId));
                    return(singleUserList);
                }

                if (access.AllPersonTypes && access.SiteId != null)
                {
                    var store = new PersonSiteDataStore(transaction);
                    IList <PersonSite> people = store.FindPersonsBySite(access.SiteId, false);
                    var userList  = new List <User>();
                    var userStore = new UserDataStore(transaction);

                    foreach (PersonSite person in people)
                    {
                        userList.Add(userStore.FindByPerson(_applicationName, person.Person.Id));
                    }

                    return(userList);
                }

                if (access.AllSites && access.PersonTypeId != null)
                {
                    var store = new PersonPersonTypeDataStore(transaction);
                    IList <PersonPersonType> people = store.FindPersonByPersonType(access.PersonTypeId);
                    var userList  = new List <User>();
                    var userStore = new UserDataStore(transaction);

                    foreach (PersonPersonType person in people)
                    {
                        userList.Add(userStore.FindByPerson(_applicationName, person.Person.Id));
                    }

                    return(userList);
                }

                if (access.SiteId != null && access.PersonTypeId != null)
                {
                    var userInPersonTypeStore = new PersonPersonTypeDataStore(transaction);
                    IList <PersonPersonType> personInPersonType =
                        userInPersonTypeStore.FindPersonByPersonType(access.PersonTypeId);

                    var userInSiteStore            = new PersonSiteDataStore(transaction);
                    IList <PersonSite> userInSites = userInSiteStore.FindPersonsBySite(access.SiteId, false);

                    //convert to a generic list to perform better search
                    var userInSitesList = new List <PersonSite>(userInSites);

                    var usersInBoth = new List <User>();

                    var userStore = new UserDataStore(transaction);

                    foreach (PersonPersonType personPersonType in personInPersonType)
                    {
                        if (
                            userInSitesList.Exists(
                                delegate(PersonSite ul) { return(ul.Person.Id == personPersonType.Person.Id); }))
                        {
                            usersInBoth.Add(userStore.FindByName(_applicationName, personPersonType.Person.Id));
                        }
                    }

                    return(usersInBoth);
                }

                return(new List <User>());
            }
        }
        public override bool ValidateUser(string username, string password)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserDataStore dataStore = new UserDataStore(transaction);
                User dbUser = dataStore.FindByName(ApplicationName, username);
                if (dbUser == null)
                    return false; //throw new UserNotFoundException(username);

                bool valid = dbUser.Login(password, PasswordAttemptWindow, MaxInvalidPasswordAttempts);

                transaction.Commit();

                return valid;
            }
        }
        public override void UpdateUser(System.Web.Security.MembershipUser user)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserDataStore dataStore = new UserDataStore(transaction);
                User dbUser = dataStore.FindByName(ApplicationName, user.UserName);
                if (dbUser == null)
                    throw new UserNotFoundException(user.UserName);

                //Check email
                if (RequiresUniqueEmail)
                {
                    if (user.Email == null || user.Email.Length == 0)
                    {
                        throw new EMailNotValidException(user.Email);
                    }

                    IList<User> emailUsers = dataStore.FindByEmail(ApplicationName, user.Email);
                    if (emailUsers.Count > 0 && emailUsers[0].Id != dbUser.Id)
                    {
                        throw new EMailDuplucatedException(user.Email);
                    }
                }

                dbUser.Comment = user.Comment;
                dbUser.EMail = user.Email;
                dbUser.Enabled = user.IsApproved;

                transaction.Commit();
            }
        }
        public override bool UnlockUser(string userName)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserDataStore dataStore = new UserDataStore(transaction);
                User user = dataStore.FindByName(ApplicationName, userName);
                if (user == null)
                    throw new UserNotFoundException(userName);

                user.Unlock();

                transaction.Commit();
            }

            return true;
        }
        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException();
            }

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserDataStore dataStore = new UserDataStore(transaction);
                User user = dataStore.FindByName(ApplicationName, username);
                if (user == null)
                    throw new UserNotFoundException(username);

                if (RequiresQuestionAndAnswer &&
                    user.ValidatePasswordAnswer(answer, PasswordAttemptWindow, MaxInvalidPasswordAttempts) == false)
                {
                    transaction.Commit();

                    throw new MembershipPasswordException();
                }
                else
                {
                    string newPassword = System.Web.Security.Membership.GeneratePassword(MinRequiredPasswordLength, MinRequiredNonAlphanumericCharacters);
                    user.ChangePassword(newPassword);

                    transaction.Commit();

                    return newPassword;
                }
            }
        }
        public override System.Web.Security.MembershipUser GetUser(string username, bool userIsOnline)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserDataStore dataStore = new UserDataStore(transaction);
                User user = dataStore.FindByName(ApplicationName, username);
                if (user == null)
                    return null;

                if (userIsOnline)
                    user.LastActivityDate = DateTime.Now;

                transaction.Commit();

                return UserToMembershipUser(user);
            }
        }
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope(mConfiguration))
                {
                    if (deleteAllRelatedData)
                    {
                        Roles.UserInRoleDataStore userInRoleStore = new Eucalypto.Roles.UserInRoleDataStore(transaction);
                        IList<Roles.UserInRole> userInRoles = userInRoleStore.FindForUser(ApplicationName, username);
                        foreach (Roles.UserInRole ur in userInRoles)
                            userInRoleStore.Delete(ur.Id);
                    }

                    UserDataStore dataStore = new UserDataStore(transaction);
                    User user = dataStore.FindByName(ApplicationName, username);
                    if (user == null)
                        throw new UserNotFoundException(username);

                    dataStore.Delete(user.Id);

                    transaction.Commit();
                }

                return true;
            }
            catch (Exception ex)
            {
                LogException(ex, "DeleteUser");
                return false;
            }
        }
        /// <summary>
        /// 
        /// </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">Not used</param>
        /// <param name="status"></param>
        /// <returns></returns>
        public override System.Web.Security.MembershipUser CreateUser(string username, string password, 
                                                                    string email, string passwordQuestion, 
                                                                    string passwordAnswer, bool isApproved, 
                                                                    object providerUserKey, 
                                                                    out System.Web.Security.MembershipCreateStatus status)
        {
            try
            {
                //Validate password
                ValidatePassword(username, password, true);

                using (TransactionScope transaction = new TransactionScope(mConfiguration))
                {
                    UserDataStore dataStore = new UserDataStore(transaction);

                    //Check name
                    if (dataStore.FindByName(ApplicationName, username) != null)
                    {
                        status = MembershipCreateStatus.DuplicateUserName;
                        return null;
                    }

                    //Check email
                    if (RequiresUniqueEmail)
                    {
                        if (email == null || email.Length == 0)
                        {
                            status = MembershipCreateStatus.InvalidEmail;
                            return null;
                        }
                        if (dataStore.FindByEmail(ApplicationName, email).Count > 0)
                        {
                            status = MembershipCreateStatus.DuplicateEmail;
                            return null;
                        }
                    }

                    User user = new User(ApplicationName, username);
                    user.EMail = email;
                    user.ChangePassword(password);
                    user.ChangePasswordQuestionAnswer(passwordQuestion, passwordAnswer);
                    user.Enabled = isApproved;

                    dataStore.Insert(user);

                    transaction.Commit();

                    status = MembershipCreateStatus.Success;
                    return UserToMembershipUser(user);
                }
            }
            catch (CodeInvalidCharsException ex) //this exception is caused by an invalid user Name
            {
                LogException(ex, "CreateUser");
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            catch (MembershipPasswordException ex)
            {
                LogException(ex, "CreateUser");
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }
            catch (Exception ex)
            {
                LogException(ex, "CreateUser");
                status = MembershipCreateStatus.ProviderError;
                return null;
            }
        }
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope(mConfiguration))
                {
                    UserDataStore dataStore = new UserDataStore(transaction);
                    User user = dataStore.FindByName(ApplicationName, username);
                    if (user == null)
                        throw new UserNotFoundException(username);

                    if (user.CheckPassword(password) == false)
                        throw new UserNotFoundException(username);

                    user.ChangePasswordQuestionAnswer(newPasswordQuestion, newPasswordAnswer);

                    transaction.Commit();
                }

                return true;
            }
            catch (Exception ex)
            {
                LogException(ex, "ChangePasswordQuestionAndAnswer");
                return false;
            }
        }