Example #1
0
        /// <summary>
        /// Remove an external login of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="loginProvider">The login provider name.</param>
        /// <param name="providerKey">The login provider key.</param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveLoginAsync(U user, string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (loginProvider == null)
            {
                throw new ArgumentNullException(nameof(loginProvider));
            }
            if (providerKey == null)
            {
                throw new ArgumentNullException(nameof(providerKey));
            }

            var registrationProvider = GetRegistrationProvider(loginProvider);

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registrationFound =
                    user.Registrations.FirstOrDefault(registration =>
                                                      registration.ProviderKey == providerKey && registration.Provider == registrationProvider);

                if (registrationFound != null)
                {
                    await OnRemovingLoginAsync(registrationFound);

                    this.DomainContainer.Registrations.Remove(registrationFound);

                    await DomainContainer.SaveChangesAsync();
                }

                transaction.Commit();
            }
        }
        /// <summary>
        /// Remove a role from a user. If the role does not exist
        /// or the user doesn't havit, nothing happens.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">The name of the role to remove from the user.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveFromRoleAsync(IdentityUser <U> user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                var removedRole = domainUser.Roles.Where(r => r.Name == roleName).FirstOrDefault();

                if (removedRole == null)
                {
                    return;
                }

                domainUser.Roles.Add(removedRole);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
Example #3
0
        /// <summary>
        /// Add a <see cref="Registration"/> to a user of type <typeparamref name="U"/>.
        /// </summary>
        /// <param name="user">
        /// The user.
        /// </param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <param name="login">
        /// The <see cref="UserLoginInfo"/>
        /// that corresponds to the <see cref="Registration"/>.
        /// </param>
        /// <returns>Returns the task which completes the operation.</returns>
        public virtual async Task AddLoginAsync(U user, UserLoginInfo login, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registration = DomainContainer.Registrations.Create();

                RegistrationProvider registrationProvider;

                registrationProvider = GetRegistrationProvider(login);

                registration.Provider    = registrationProvider;
                registration.ProviderKey = login.ProviderKey;
                registration.User        = user;

                await OnAddingLoginAsync(registration);

                DomainContainer.Registrations.Add(registration);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
        /// <summary>
        /// Remove an external login of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="login">The representation of an external login.</param>
        /// <returns>Returns a task for the operation.</returns>
        public virtual async Task RemoveLoginAsync(IdentityUser <U> user, UserLoginInfo login)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            var registrationProvider = GetRegistrationProvider(login);

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var registrationFound =
                    user.DomainUser.Registrations.FirstOrDefault(registration =>
                                                                 registration.ProviderKey == login.ProviderKey && registration.Provider == registrationProvider);

                if (registrationFound != null)
                {
                    await OnRemovingLoginAsync(registrationFound);

                    this.DomainContainer.Registrations.Remove(registrationFound);

                    await DomainContainer.SaveChangesAsync();
                }

                transaction.Commit();
            }
        }
Example #5
0
        /// <summary>
        /// Set the e-mail of a user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="email">the user's e-mail.</param>
        /// <param name="cancellationToken">Cancellation token for the operation.</param>
        /// <returns>Returns a task which completes the operation.</returns>
        public virtual async Task SetEmailAsync(U user, string email, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (email == null)
            {
                throw new ArgumentNullException(nameof(email));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                user.Email = email;

                await OnSettingEmailAsync(user);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }
        }
Example #6
0
        /// <summary>
        /// Update a user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="cancellationToken">Cancellation token used during saving.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnUpdatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task <IdentityResult> UpdateAsync(U user, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(user);

                await OnUpdatingUserAsync(user);

                DomainContainer.SetAsModified(user);

                await DomainContainer.SaveChangesAsync(cancellationToken);

                transaction.Commit();
            }

            return(IdentityResult.Success);
        }
        /// <summary>
        /// Update a user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnUpdatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task UpdateAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                await OnUpdatingUserAsync(domainUser);

                DomainContainer.SetAsModified(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
        /// <summary>
        /// Delete a user.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnDeletingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="DeletingUser"/> event during this method.
        /// </remarks>
        public virtual async Task DeleteAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                await OnDeletingUserAsync(domainUser);

                DomainContainer.Users.Remove(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
        /// <summary>
        /// Set whether a user's e-mail is confirmed.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="confirmed">True if the user's e-mail is confirmed.</param>
        /// <returns>Returns a task which completes the operation.</returns>
        public virtual async Task SetEmailConfirmedAsync(IdentityUser <U> user, bool confirmed)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (var transaction = DomainContainer.BeginTransaction())
            {
                var domainUser = user.DomainUser;

                if (domainUser.RegistrationStatus == RegistrationStatus.PendingVerification)
                {
                    domainUser.RegistrationStatus = RegistrationStatus.Verified;

                    await OnConfirmingEmailAsync(domainUser);

                    await DomainContainer.SaveChangesAsync();

                    transaction.Commit();
                }
            }
        }
        /// <summary>
        /// Add a role to a user. The role must exist in the system.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">The name of the role to add.</param>
        /// <returns>Returns a task completing the operation.</returns>
        /// <exception cref="IdentityException">
        /// Thrown when a role having the given <paramref name="roleName"/>
        /// does not exist in the system.
        /// </exception>
        public virtual async Task AddToRoleAsync(IdentityUser <U> user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Attach(domainUser);

                if (domainUser.Roles.Any(r => r.Name == roleName))
                {
                    return;
                }

                var role = await FindRoleAsync(roleName);

                if (role == null)
                {
                    throw new IdentityException($"The role '{roleName}' does not exist in the system.");
                }

                domainUser.Roles.Add(role);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }
        /// <summary>
        /// Create a user.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <returns>Returns the task which completes the operation.</returns>
        /// <remarks>
        /// <see cref="OnCreatingUserAsync"/> is invoked whose default implementation
        /// fires the <see cref="CreatingUser"/> event during this method.
        /// </remarks>
        public virtual async Task CreateAsync(IdentityUser <U> user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var domainUser = user.DomainUser;

            using (var transaction = DomainContainer.BeginTransaction())
            {
                DomainContainer.Users.Add(domainUser);

                domainUser.SecurityStamp = String.Empty;
                domainUser.CreationDate  = DateTime.UtcNow;
                domainUser.Guid          = new Guid();

                await OnCreatingUserAsync(domainUser);

                await DomainContainer.SaveChangesAsync();

                transaction.Commit();
            }
        }