Exemplo n.º 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);
                    }
                }
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     _roleService.AssignRoles(usernames, roleNames);
 }