Esempio n. 1
0
        public static async Task <Response <AspNetUser> > CreateAspNetUserAsync(RepoUnitOfWork repoUnitOfWork, CreateUserModel model)
        {
            var response = ResponseFactory.Error <AspNetUser>();

            if (repoUnitOfWork == null)
            {
                repoUnitOfWork = RepoUnitOfWork.New();
            }
            var aspNetUserRepository = repoUnitOfWork.TrackingRepository <AspNetUserRepository>();

            if (!await IsEmailNotUsedAsync(model.Email).ConfigureAwait(false))
            {
                return(response);
            }
            var aspNetUser = new AspNetUser
            {
                Id            = Guid.NewGuid().ToString(),
                UserName      = model.Email,
                PasswordHash  = model.PasswordHash,
                SecurityStamp = Guid.NewGuid().ToString("D"),
            };

            var createdAspNetUser = await aspNetUserRepository.CreateAsync(aspNetUser, navigationProperties : new[]
            {
                nameof(AspNetRole)
            }).ConfigureAwait(false);

            if (createdAspNetUser == null)
            {
                return(response);
            }
            return(ResponseFactory.Success(createdAspNetUser));
        }
Esempio n. 2
0
        internal static async Task <Response> ManageUserRoleAsync(AspNetUser aspNetUser, string roleId, RepoUnitOfWork unitOfWork, int handlingAction = (int)HandlingAction.ADD)
        {
            var aspNetUserRoleRepository = unitOfWork.TrackingRepository <AspNetRoleRepository>();

            var aspNetRole = await aspNetUserRoleRepository.GetAsync(roleId).ConfigureAwait(false);

            if (aspNetRole == null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            if (handlingAction == (int)HandlingAction.ADD)
            {
                if (aspNetUser.AspNetRoles.Any(role => role.Id == roleId))
                {
                    return(ResponseFactory.SuccessResponse);
                }
                aspNetUser.AspNetRoles.Add(aspNetRole);
            }
            else if (handlingAction == (int)HandlingAction.REMOVE)
            {
                if (aspNetUser.AspNetRoles.All(role => role.Id != roleId))
                {
                    return(ResponseFactory.SuccessResponse);
                }
                aspNetUser.AspNetRoles.Remove(aspNetRole);
            }

            var aspNetUserRepository = unitOfWork.TrackingRepository <AspNetUserRepository>();
            var updatedAspNetUser    = await aspNetUserRepository.UpdateAsync(aspNetUser).ConfigureAwait(false);

            if (updatedAspNetUser == null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            return(ResponseFactory.SuccessResponse);
        }