示例#1
0
        /// <summary>
        /// Update only the information related to the user profile.
        /// </summary>
        /// <param name="adminUser">The admin user instance.</param>
        public void UpdateProfile(AdminUserEntity adminUser)
        {
            using (var connection = _dbConnectionFactory.CreateConnection())
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    var parameters = new DynamicParameters();

                    parameters.Add("@Id", adminUser.Id, DbType.Int32);

                    parameters.Add("@UserName", adminUser.UserName, DbType.String);
                    parameters.Add("@FullName", adminUser.FullName, DbType.String);
                    parameters.Add("@Email", adminUser.Email, DbType.String);

                    parameters.Add("@ScreenAutoLockMinutes", adminUser.ScreenAutoLockMinutes, DbType.Int32);
                    parameters.Add("@PictureBlobId", adminUser.PictureBlobId, DbType.Guid);
                    parameters.Add("@TimeZoneId", adminUser.TimeZoneId, DbType.String);
                    parameters.Add("@UICultureId", adminUser.UICultureId, DbType.String);
                    parameters.Add("@CultureId", adminUser.CultureId, DbType.String);

                    int result = connection.Execute(
                        sql: "UsersWithAdminProfileUpdate",
                        commandType: CommandType.StoredProcedure,
                        transaction: transaction,
                        param: parameters
                        );

                    transaction.Commit();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <param name="adminUser">The new user instance.</param>
        /// <returns>Returns the user instance with the updated id.</returns>
        public AdminUserEntity Create(AdminUserEntity adminUser)
        {
            using (var connection = _dbConnectionFactory.CreateConnection())
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    var parameters = new DynamicParameters();

                    parameters.Add("@MobilePhone", adminUser.MobilePhone, DbType.String);
                    parameters.Add("@LockoutEnabled", adminUser.LockoutEnabled, DbType.Boolean);
                    parameters.Add("@TwoFactorEnabled", adminUser.TwoFactorEnabled, DbType.Boolean);
                    parameters.Add("@MobilePhoneConfirmed", adminUser.MobilePhoneConfirmed, DbType.Boolean);
                    parameters.Add("@LockoutEndDateUtc", adminUser.LockoutEndDateUtc, DbType.DateTimeOffset);
                    parameters.Add("@AccessFailedCount", adminUser.AccessFailedCount, DbType.Int32);
                    parameters.Add("@EmailConfirmed", adminUser.EmailConfirmed, DbType.Boolean);
                    parameters.Add("@PasswordHash", adminUser.PasswordHash, DbType.String);
                    parameters.Add("@SecurityStamp", adminUser.SecurityStamp, DbType.String);
                    parameters.Add("@UserName", adminUser.UserName, DbType.String);
                    parameters.Add("@FullName", adminUser.FullName, DbType.String);
                    parameters.Add("@Email", adminUser.Email, DbType.String);

                    parameters.Add("@TimeZoneId", adminUser.TimeZoneId, DbType.String);
                    parameters.Add("@UICultureId", adminUser.UICultureId, DbType.String);
                    parameters.Add("@CultureId", adminUser.CultureId, DbType.String);

                    adminUser.Id = connection.ExecuteScalar <int>(
                        sql: "UsersWithAdminProfileInsert",
                        commandType: CommandType.StoredProcedure,
                        transaction: transaction,
                        param: parameters
                        );

                    UserClaimsSet(connection, transaction, claims: adminUser.Claims, user: adminUser, cleanup: false);
                    UserRealmsSet(connection, transaction, realms: adminUser.Realms, user: adminUser, cleanup: false);
                    UserRolesSet(connection, transaction, roles: adminUser.Roles, user: adminUser, cleanup: false);

                    transaction.Commit();

                    return(adminUser);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Manages the admin user related roles.
        /// </summary>
        /// <param name="dbConnection">Current database connection.</param>
        /// <param name="transaction">Current database transaction.</param>
        /// <param name="roles">The current set of roles for the user.</param>
        /// <param name="user">The user begin managed.</param>
        /// <param name="cleanup">Remove previous roles before adding the new ones.</param>
        private void UserRolesSet(IDbConnection dbConnection, IDbTransaction transaction, List <Role> roles, AdminUserEntity user, bool cleanup)
        {
            if (cleanup)
            {
                var parameters = new DynamicParameters();

                parameters.Add(@"UserId", user.Id, DbType.Int32);

                dbConnection.Execute(
                    sql: "DELETE FROM [UserRoles] WHERE UserId = @UserId",
                    commandType: CommandType.Text,
                    transaction: transaction,
                    param: parameters
                    );
            }

            foreach (var role in roles)
            {
                var parameters = new DynamicParameters();

                parameters.Add("@UserId", user.Id, DbType.Int32);
                parameters.Add("@Role", role.ToString(), DbType.String);

                dbConnection.Query(
                    sql: "INSERT INTO [UserRoles] (UserId, [Role]) VALUES (@UserId, @Role)",
                    commandType: CommandType.Text,
                    transaction: transaction,
                    param: parameters
                    );
            }
        }
示例#4
0
        /// <summary>
        /// Manages the admin user related claims.
        /// </summary>
        /// <param name="dbConnection">Current database connection.</param>
        /// <param name="transaction">Current database transaction.</param>
        /// <param name="claims">The current set of claims for the user.</param>
        /// <param name="user">The user begin managed.</param>
        /// <param name="cleanup">Remove previous claims before adding the new ones.</param>
        private void UserClaimsSet(IDbConnection dbConnection, IDbTransaction transaction, List <Claim> claims, AdminUserEntity user, bool cleanup)
        {
            if (cleanup)
            {
                var parameters = new DynamicParameters();

                parameters.Add(@"UserId", user.Id, DbType.Int32);

                dbConnection.Execute(
                    sql: "DELETE FROM [UserClaims] WHERE UserId = @UserId",
                    commandType: CommandType.Text,
                    transaction: transaction,
                    param: parameters
                    );
            }

            foreach (var claim in claims)
            {
                var parameters = new DynamicParameters();

                parameters.Add("@UserId", user.Id, DbType.Int32);
                parameters.Add("@Type", claim.Type, DbType.String);
                parameters.Add("@Value", claim.Value, DbType.String);

                dbConnection.Query(
                    sql: "INSERT INTO [UserClaims] (UserId, [Type], [Value]) VALUES (@UserId, @Type, @Value)",
                    commandType: CommandType.Text,
                    transaction: transaction,
                    param: parameters
                    );
            }
        }