Ejemplo n.º 1
0
        public async Task <dynamic> MigrateAsync(string connectionString, Membership _membership, UserWithPassword _user)
        {
            // Validation
            var databaseInformation = Ertis.MongoDB.Helpers.ConnectionStringHelper.ParseConnectionString(connectionString);
            var connectionString1   = Ertis.MongoDB.Helpers.ConnectionStringHelper.GenerateConnectionString(this.databaseSettings);
            var connectionString2   = Ertis.MongoDB.Helpers.ConnectionStringHelper.GenerateConnectionString(databaseInformation);

            if (connectionString1 != connectionString2)
            {
                throw ErtisAuthException.MigrationRejected("Connection string could not validated");
            }

            // 1. Membership
            var membership = await this.membershipService.CreateAsync(new Membership
            {
                Name                  = _membership.Name,
                DefaultEncoding       = _membership.DefaultEncoding,
                HashAlgorithm         = _membership.HashAlgorithm,
                ExpiresIn             = _membership.ExpiresIn,
                RefreshTokenExpiresIn = _membership.RefreshTokenExpiresIn,
                SecretKey             = string.IsNullOrEmpty(_membership.SecretKey) ? GenerateRandomSecretKey(32) : _membership.SecretKey
            });

            // Utilizer
            var utilizer = new Utilizer
            {
                Role         = Rbac.ReservedRoles.Administrator,
                Type         = Utilizer.UtilizerType.System,
                MembershipId = membership.Id
            };

            // 2. Role
            var adminRole = await this.roleService.CreateAsync(utilizer, membership.Id, new Role
            {
                Name         = Rbac.ReservedRoles.Administrator,
                Description  = "Administrator",
                MembershipId = membership.Id,
                Permissions  = RoleHelper.AssertAdminPermissionsForReservedResources()
            });

            // 3. User (admin)
            var adminUser = await this.userService.CreateAsync(utilizer, membership.Id, new UserWithPassword
            {
                Username     = _user.Username,
                FirstName    = _user.FirstName,
                LastName     = _user.LastName,
                EmailAddress = _user.EmailAddress,
                Role         = adminRole.Name,
                MembershipId = membership.Id,
                PasswordHash = this.cryptographyService.CalculatePasswordHash(membership, _user.PasswordHash)
            });

            return(new
            {
                membership,
                user = adminUser,
                role = adminRole
            });
        }