Beispiel #1
0
        /// <summary>
        /// Performs the persistence for the member
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public virtual async Task UpdateAsync(TMember user)
        {
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var asInt = user.Id.TryConvertTo <int>();

            if (!asInt)
            {
                throw new InvalidOperationException("The user id must be an integer to work with the UmbracoMembersUserStore");
            }

            var found = _memberService.GetById(asInt.Result);

            if (found != null)
            {
                if (UpdateMemberProperties(found, user))
                {
                    _memberService.Save(found);
                }

                if (user.LoginsChanged)
                {
                    var logins = await GetLoginsAsync(user);

                    _externalLoginStore.SaveUserLogins(found.Id, logins);
                }

                if (user.RolesChanged)
                {
                    IMembershipRoleService <IMember> memberRoleService = _memberService;

                    var persistedRoles = memberRoleService.GetAllRoles(found.Id).ToArray();
                    var userRoles      = user.Roles.Select(x => x.RoleName).ToArray();

                    var keep   = persistedRoles.Intersect(userRoles).ToArray();
                    var remove = persistedRoles.Except(keep).ToArray();
                    var add    = userRoles.Except(persistedRoles).ToArray();

                    if (remove.Any())
                    {
                        memberRoleService.DissociateRoles(new[] { found.Id }, remove);
                    }
                    if (add.Any())
                    {
                        memberRoleService.AssignRoles(new[] { found.Id }, add);
                    }
                }
            }
        }
Beispiel #2
0
        public virtual async Task CreateAsync(TMember user)
        {
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var member = _memberService.CreateMember(
                user.UserName, user.Email,
                user.Name.IsNullOrWhiteSpace() ? user.UserName : user.Name,
                user.MemberTypeAlias.IsNullOrWhiteSpace() ? _membershipProvider.DefaultMemberTypeAlias: user.MemberTypeAlias);

            UpdateMemberProperties(member, user);

            //the password must be 'something' it could be empty if authenticating
            // with an external provider so we'll just generate one and prefix it, the
            // prefix will help us determine if the password hasn't actually been specified yet.
            if (member.RawPasswordValue.IsNullOrWhiteSpace())
            {
                //this will hash the guid with a salt so should be nicely random
                var aspHasher = new PasswordHasher();
                member.RawPasswordValue = _emptyPasswordPrefix +
                                          aspHasher.HashPassword(Guid.NewGuid().ToString("N"));
            }
            _memberService.Save(member);

            //re-assign id
            user.Id = member.Id;

            if (user.LoginsChanged)
            {
                var logins = await GetLoginsAsync(user);

                _externalLoginStore.SaveUserLogins(member.Id, logins);
            }

            if (user.RolesChanged)
            {
                IMembershipRoleService <IMember> memberRoleService = _memberService;

                var persistedRoles = memberRoleService.GetAllRoles(member.Id).ToArray();
                var userRoles      = user.Roles.Select(x => x.RoleName).ToArray();

                var keep   = persistedRoles.Intersect(userRoles).ToArray();
                var remove = persistedRoles.Except(keep).ToArray();
                var add    = userRoles.Except(persistedRoles).ToArray();

                memberRoleService.DissociateRoles(new[] { member.Id }, remove);
                memberRoleService.AssignRoles(new[] { member.Id }, add);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This is used to assign the lazy callbacks for the user's data collections
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private TMember AssignUserDataCallback(TMember user)
        {
            if (user != null)
            {
                user.SetLoginsCallback(new Lazy <IEnumerable <IdentityMemberLogin <int> > >(() =>
                                                                                            _externalLoginStore.GetAll(user.Id)));

                user.SetRolesCallback(new Lazy <IEnumerable <IdentityMemberRole <int> > >(() =>
                {
                    IMembershipRoleService <IMember> memberRoleService = _memberService;
                    var roles = memberRoleService.GetAllRoles(user.Id);
                    return(roles.Select(x => new IdentityMemberRole()
                    {
                        RoleName = x,
                        UserId = user.Id
                    }));
                }));
            }
            return(user);
        }
Beispiel #4
0
 public override string[] GetRolesForUser(string username)
 {
     return(_roleService.GetAllRoles(username).ToArray());
 }