Exemple #1
0
        /// <summary>
        /// Добавляет заданного пользователя в хранилище данных
        /// и возвращает идентификатор добавленного пользователя.
        /// </summary>
        /// <param name="user">Экземпляр пользователя для добавления.</param>
        /// <returns>Идентификатор пользователя.</returns>
        public async Task <IdentityResult> AddUser(Common.Models.User.User user)
        {
            //user.AccountLevel = _userAccountLevelService.GetUserAccountLevel(1);
            var dbUser = Mapper.Map <DbUser>(user);

            dbUser.Id = Guid.NewGuid().ToString();

            var DbUserProfile = new DbUserProfile()
            {
                Email = new Collection <DbEmail>()
                {
                    new DbEmail()
                    {
                        Name = "emptyEmail",
                    }
                },
            };

            dbUser.DbUserProfile = DbUserProfile;

            dbUser.Email             = "emptyEmail";
            dbUser.PhoneNumber       = "emptyPhoneNumber";
            dbUser.TwoFactorEnabled  = false;
            dbUser.LockoutEnabled    = false;
            dbUser.AccessFailedCount = 0;

            var result = await _usermanager.CreateAsync(dbUser)
                         .ConfigureAwait(false);

            return(result);

            throw new InvalidOperationException(result.Errors.Aggregate((a, b) => $"{a} {b}"));
        }
        /// <summary>
        /// Добавляет заданного пользователя в хранилище данных
        /// и возвращает идентификатор добавленного пользователя.
        /// </summary>
        /// <param name="user">Экземпляр пользователя для добавления.</param>
        /// <returns>Идентификатор пользователя.</returns>
        public async Task <IdentityResult> AddUser(Common.Models.User.User user)
        {
            var dbUser = Mapper.Map <DbUser>(user);

            dbUser.Id = Guid.NewGuid().ToString();

            var DbUserProfile = new DbUserProfile()
            {
                Email = new Collection <DbEmail>()
                {
                    new DbEmail()
                    {
                        Name = "emptyEmail",
                    }
                },
            };

            dbUser.DbUserProfile = DbUserProfile;

            dbUser.Email             = "emptyEmail";
            dbUser.PhoneNumber       = "emptyPhoneNumber";
            dbUser.TwoFactorEnabled  = false;
            dbUser.LockoutEnabled    = false;
            dbUser.AccessFailedCount = 0;

            var result = await _usermanager.CreateAsync(dbUser)
                         .ConfigureAwait(false);

            return(result);
        }
        /// <summary>
        /// Добавляет заданного пользователя в хранилище данных
        /// и возвращает идентификатор добавленного пользователя.
        /// </summary>
        /// <param name="user">Экземпляр пользователя для добавления.</param>
        /// <returns>Идентификатор пользователя.</returns>
        public async Task <IdentityResult> AddUser(Common.Models.User.User user)
        {
            var dbUser = Mapper.Map <DbUser>(user);

            dbUser.Id = Guid.NewGuid().ToString();

            var dbUserProfile = new DbUserProfile
            {
                Email = new Collection <DbEmail>
                {
                    new DbEmail
                    {
                        Name = Resources.EMPTY_EMAIL_DATA,
                    }
                }
            };

            dbUser.TwoFactorEnabled  = false;
            dbUser.LockoutEnabled    = false;
            dbUser.AccessFailedCount = 0;
            dbUser.DbUserProfile     = dbUserProfile;
            dbUser.UserName          = user.AccountLevel.Name;
            dbUser.Email             = Resources.EMPTY_EMAIL_DATA;
            dbUser.PhoneNumber       = Resources.EMPTY_PHONE_DATA;

            var result = await _usermanager.CreateAsync(dbUser, user.Password)
                         .ConfigureAwait(false);

            return(result);
        }
Exemple #4
0
        public async Task <IHttpActionResult> AddUser([FromBody] BL.Common.Models.User.User user)
        {
            if (user == null)
            {
                return(BadRequest("User can not be empty"));
            }

            var result = await _rocketUserManagerService.FindByNameAsync(user.Login).ConfigureAwait(false);

            if (result != null)
            {
                return(BadRequest("User exists"));
            }

            var dbRole = await _rolemanager.FindByNameAsync("administrator").ConfigureAwait(false);

            var dbUserProfile = new DbUserProfile()
            {
                Email = new Collection <DbEmail>()
                {
                    new DbEmail()
                    {
                        Name = "emptyEmail",
                    }
                },
            };

            var dbUser = Mapper.Map <DbUser>(user);

            dbUser.DbUserProfile = dbUserProfile;

            await _rocketUserManagerService.CreateAsync(dbUser).ConfigureAwait(false);

            await _rocketUserManagerService
            .AddToRoleAsync(dbUser.Id, "user").ConfigureAwait(false);

            return(Ok());
        }
Exemple #5
0
        public async Task <IActionResult> CreateUserByDeviceId([FromBody] UserDevice userDevice)
        {
            if (string.IsNullOrEmpty(userDevice?.DeviceId))
            {
                return(BadRequest("DeviceId was invalid."));
            }

            // NOTE: We may want to enable this again later
            // Check if DeviceId exists
            //var dbUser = await m_CoreDbContext.Users
            //    .Where(dbu => dbu.DeviceId == userDevice.DeviceId)
            //    .SingleOrDefaultAsync();

            // If it does not exist, create a new user
            //if (dbUser == null)
            //{
            var userId    = Guid.NewGuid().ToString();
            var authToken = Guid.NewGuid().ToString();

            // User data
            var dbUser = new DbUser
            {
                UserId    = userId,
                DeviceId  = userDevice.DeviceId,
                AuthToken = authToken
            };

            // Users default profile
            var dbUserProfile = new DbUserProfile
            {
                UsersProfileId        = Guid.NewGuid().ToString(),
                UserId                = userId,
                Name                  = m_ConfigDbContext.UserDefaults.Profile.Name,
                Xp                    = m_ConfigDbContext.UserDefaults.Profile.Xp,
                CreateDate            = DateTime.UtcNow,
                LastSeenDate          = DateTime.UtcNow,
                LastDailyRewardDate   = DateTime.UtcNow,
                GoldMineDaysRemaining = 0,
                GemMineDaysRemaining  = 0,
                Level                 = 0,
                FriendCount           = 0,
            };

            // Users default wallet
            var dbUserWallet = new DbUserWallet
            {
                UsersWalletId = Guid.NewGuid().ToString(),
                UserId        = userId,
                HappyTokens   = m_ConfigDbContext.UserDefaults.Wallet.HappyTokens,
                Gems          = m_ConfigDbContext.UserDefaults.Wallet.Gems,
                Gold          = m_ConfigDbContext.UserDefaults.Wallet.Gold,
            };

            // User default happiness
            var dbUserHappiness = new DbUserHappiness
            {
                UsersHappinessId = Guid.NewGuid().ToString(),
                UserId           = userId,
                Wealth           = m_ConfigDbContext.UserDefaults.Happiness.Wealth,
                Experience       = m_ConfigDbContext.UserDefaults.Happiness.Experience,
                Health           = m_ConfigDbContext.UserDefaults.Happiness.Health,
                Skill            = m_ConfigDbContext.UserDefaults.Happiness.Skill,
                Social           = m_ConfigDbContext.UserDefaults.Happiness.Social
            };

            // Create default avatars (Avatars give happiness, allocate based on Level1)
            var dbUsersAvatars = new List <DbUserAvatar>();

            foreach (var avatarType in m_ConfigDbContext.UserDefaults.AvatarTypes)
            {
                var userAvatar = new DbUserAvatar()
                {
                    UsersAvatarId = Guid.NewGuid().ToString(),
                    UserId        = userId,
                    AvatarType    = avatarType,
                    Level         = 1,
                    Pieces        = 0
                };

                // Grab the default avatar config
                var avatar = m_ConfigDbContext.Avatars.Avatars.Find(i => i.AvatarType == avatarType);

                // Add the happiness gained from Level1 to the users Happiness
                var happinessType   = avatar.HappinessType;
                var happinessAmount = avatar.Levels[0].Happiness;
                dbUserHappiness.Add(happinessType, happinessAmount);

                dbUsersAvatars.Add(userAvatar);
            }

            // Create default buildings (Buildings give happiness, allocate based on Level1)
            var dbUsersBuildings = new List <DbUserBuilding>();

            foreach (var buildingType in m_ConfigDbContext.UserDefaults.BuildingTypes)
            {
                var userBuilding = new DbUserBuilding()
                {
                    UsersBuildingId = Guid.NewGuid().ToString(),
                    UserId          = userId,
                    BuildingType    = buildingType,
                    Level           = 1
                };

                // Grab the default avatar config
                var building = m_ConfigDbContext.Buildings.Buildings.Find(i => i.BuildingType == buildingType);

                // Add the happiness gained from Level1 to the users Happiness
                var happinessType   = building.HappinessType;
                var happinessAmount = building.Levels[0].Happiness;
                dbUserHappiness.Add(happinessType, happinessAmount);

                dbUsersBuildings.Add(userBuilding);
            }

            // Add the new user
            await m_CoreDbContext.Users.AddAsync(dbUser);

            await m_CoreDbContext.UsersProfiles.AddAsync(dbUserProfile);

            await m_CoreDbContext.UsersWallets.AddAsync(dbUserWallet);

            await m_CoreDbContext.UsersHappiness.AddAsync(dbUserHappiness);

            await m_CoreDbContext.UsersAvatars.AddRangeAsync(dbUsersAvatars);

            await m_CoreDbContext.UsersBuildings.AddRangeAsync(dbUsersBuildings);

            // Save changes
            await m_CoreDbContext.SaveChangesAsync();

            // Create the user to send back to the client
            var response = new UserAuthPair
            {
                UserId    = userId,
                AuthToken = authToken
            };

            return(Ok(response));
            // }

            // User with this DeviceId already exists
            // return Forbid();
        }