private async Task CreatePlayerIfNotExists(string nickname) { var player = await _usersDataProvider.GetBasicUser(nickname); if (player == null) { player = await _usersDataProvider.CreateUser(nickname); } }
/// <summary> /// Creates the user with specified role. /// </summary> /// <param name="user">The user.</param> /// <param name="role">Role which should be assigned to user.</param> /// <param name="customerId"></param> /// <returns></returns> public async Task <User> CreateUser(User user, string role, int customerId) { var roleEntity = await this.GetUserRole(role); if (roleEntity == null) { throw new DataNotFoundException("Role with name {0} not exists".FormatWith(role)); } var createPrincipalModel = Mapper.Map <CreatePrincipalModel>(user); createPrincipalModel.CustomerId = customerId; var principal = await tokenDataProvider.CreateUser(createPrincipalModel); user.RoleId = roleEntity.Id; user.TokenServiceUserId = principal.Id.ToString(); usersRepository.Insert(user); await unitOfWork.SaveAsync(); return(user); }
/// <summary> /// Assign specified user to customer. /// </summary> /// <param name="customerUser">The customer user.</param> /// <param name="bearerToken">The bearer token.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">customerUser</exception> /// <exception cref="DataNotFoundException">Role with name {0} not exists.FormatWith(Roles.CustomerDefaultRole)</exception> public async Task <CreateCustomerUserResultDto> CreateCustomerUser(CustomerUser customerUser, string bearerToken) { var result = new CreateCustomerUserResultDto(); if (customerUser == null) { throw new ArgumentNullException("customerUser"); } if (string.IsNullOrEmpty(bearerToken)) { throw new ArgumentNullException("bearerToken"); } var roleEntity = await usersService.GetUserRole(Roles.CustomerUser); if (roleEntity == null) { throw new DataNotFoundException("Role with name {0} not exists".FormatWith(Roles.CustomerUser)); } var existingCustomerUser = await GetCustomerUserByEmail(customerUser.Email, true); if (existingCustomerUser != null) { result.Error = CreateUpdateCustomerUserErrorState.EmailAlreadyExists; } var existingCustomer = await customersService.GetCustomer(customerUser.CustomerId, bearerToken); if (existingCustomer == null) { if (result.Error.HasValue) { result.Error |= CreateUpdateCustomerUserErrorState.CustomerDoesNotExist; } else { result.Error = CreateUpdateCustomerUserErrorState.CustomerDoesNotExist; } } else { var customerSitesIds = existingCustomer.Sites.Select(s => s.Id).ToList(); if (customerUser.CustomerUserSites != null && !customerSitesIds.Contains(customerUser.CustomerUserSites.Select(s => s.SiteId).ToArray())) { if (result.Error.HasValue) { result.Error |= CreateUpdateCustomerUserErrorState.SitesDoNotExistWithinCustomer; } else { result.Error = CreateUpdateCustomerUserErrorState.SitesDoNotExistWithinCustomer; } } } var customerUserRoles = await GetCustomerUserRolesForCustomer(customerUser.CustomerId); if (customerUserRoles.All(cr => cr.Id != customerUser.CustomerUserRoleId)) { if (result.Error.HasValue) { result.Error |= CreateUpdateCustomerUserErrorState.CustomerUserRoleDoesNotExistWithinCustomer; } else { result.Error = CreateUpdateCustomerUserErrorState.CustomerUserRoleDoesNotExistWithinCustomer; } } if (!string.IsNullOrEmpty(customerUser.CustomerUserId) || !string.IsNullOrEmpty(customerUser.NationalProviderIdentificator)) { var customerUsers = await GetCustomerUsers(customerUser.CustomerId); if (!string.IsNullOrEmpty(customerUser.CustomerUserId) && customerUsers.Any(cu => cu.CustomerUserId == customerUser.CustomerUserId)) { if (result.Error.HasValue) { result.Error |= CreateUpdateCustomerUserErrorState.CustomerUserIdAlreadyExists; } else { result.Error = CreateUpdateCustomerUserErrorState.CustomerUserIdAlreadyExists; } } if (!string.IsNullOrEmpty(customerUser.NationalProviderIdentificator) && customerUsers.Any( cu => cu.NationalProviderIdentificator == customerUser.NationalProviderIdentificator)) { if (result.Error.HasValue) { result.Error |= CreateUpdateCustomerUserErrorState.NPIAlreadyExists; } else { result.Error = CreateUpdateCustomerUserErrorState.NPIAlreadyExists; } } } if (result.IsValid) { var principal = await tokenDataProvider.CreateUser(Mapper.Map <CreatePrincipalModel>(customerUser)); customerUser.RoleId = roleEntity.Id; customerUser.TokenServiceUserId = principal.Id.ToString(); customerUsersRepository.Insert(customerUser); await unitOfWork.SaveAsync(); var customerUserRole = await GetCustomerUserPermissions(customerUser.Id); await usersService.SetUserGroupsByPermissions(customerUserRole.Permissions, customerUser); var userSites = customerUser.CustomerUserSites != null? customerUser.CustomerUserSites.Select(s => s.SiteId).ToList() : new List <Guid>(); await SetSites(customerUser.Id, userSites); result.Id = customerUser.Id; } return(result); }