Пример #1
0
        /// <summary>
        ///     Initializes User collection in database with demo values.
        /// </summary>
        /// <author>Anna Krebs</author>
        /// <param name="roleValue"></param>
        /// <returns></returns>
        private async Task InitRoleCollection(RoleValue roleValue)
        {
            var roleValueString = roleValue.ToString();

            var roleCollection = _mongoContext.Database.GetCollection <Role>(nameof(Role));
            var isDuplicate    =
                await MongoHelper <Role> .IsDuplicate(Constants.Name, roleValueString, roleCollection, null);

            if (!isDuplicate)
            {
                var role = new Role {
                    Name = roleValueString
                };
                await roleCollection.InsertOneAsync(role);

                Log.Info($"Inserted role with name {role} with Id {role.ObjectId} into database.");
            }
        }
Пример #2
0
        /// <summary>
        ///     Initializes User collection in database with demo values.
        /// </summary>
        /// <author>Anna Krebs</author>
        /// <param name="email"></param>
        /// <param name="passwordHash"></param>
        /// <param name="isAccountVerified"></param>
        /// <param name="isConfirmed"></param>
        /// <param name="isInactive"></param>
        /// <param name="roleValue"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="isSystemAdmin"></param>
        /// <returns></returns>
        private async Task InitUserCollection(string email, string passwordHash, bool isAccountVerified,
                                              bool isConfirmed, bool isInactive, RoleValue roleValue, string firstName, string lastName,
                                              bool isSystemAdmin)
        {
            var roleValueString = roleValue.ToString();

            var userCollection = _mongoContext.Database.GetCollection <User>(nameof(User));
            var isDuplicate    = await MongoHelper <User> .IsDuplicate(Constants.Email, email, userCollection, null);

            if (!isDuplicate)
            {
                // Get role by name.
                var roleCollection = _mongoContext.Database.GetCollection <Role>(nameof(Role));
                var bsonFilter     = Builders <Role> .Filter.Eq(nameof(Role.Name), roleValueString);

                var role = await roleCollection.Find(bsonFilter, _findOptions).SingleOrDefaultAsync();

                var user = new User
                {
                    FirstName         = firstName,
                    LastName          = lastName,
                    Email             = email,
                    PasswordHash      = passwordHash,
                    IsAccountVerified = isAccountVerified,
                    IsConfirmed       = isConfirmed,
                    IsInactive        = isInactive,
                    IsSystemAdmin     = isSystemAdmin,
                    RoleId            = role.ObjectId,
                    PasswordResetKey  = null
                };

                await userCollection.InsertOneAsync(user);

                Log.Info($"Inserted user with email {email} with Id {user.ObjectId} into database.");
            }
        }