protected async Task SetNameAsync(Material material, string name)
        {
            if (User.IsInRole(RoleNames.Admin))
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    material.Name = null;
                }
                else
                {
                    if (!materialsManager.IsNameValid(name))
                    {
                        throw new SunViewException(new ErrorView("MaterialNameNotValid", "Invalid material name",
                                                                 ErrorType.System));
                    }

                    if (name != material.Name && await materialsManager.IsNameInDbAsync(name))
                    {
                        throw new SunViewException(ErrorView.SoftError("MaterialNameAlreadyUsed",
                                                                       "This material name is already used"));
                    }

                    material.Name = name;
                }
            }
        }
        protected async Task <ServiceResult> SetNameAsync(Material material, string name)
        {
            if (User.IsInRole(RoleNames.Admin))
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    material.Name = null;
                }
                else
                {
                    if (!materialsManager.IsNameValid(name))
                    {
                        return(ServiceResult.BadResult(new ErrorView("MaterialNameNotValid", "Invalid material name")));
                    }

                    if (name != material.Name && await materialsManager.IsNameInDb(name))
                    {
                        return(ServiceResult.BadResult(ErrorView.SoftError("MaterialNameAlreadyUsed",
                                                                           "This material name is already used")));
                    }

                    material.Name = name;
                }
            }

            return(ServiceResult.OkResult());
        }
        public async Task <IActionResult> ChangeEmail(string password, string email)
        {
            email = email.Trim();

            if (!EmailValidator.IsValid(email))
            {
                return(BadRequest(ErrorView.SoftError("EmailInvalid", "Email not valid")));
            }

            var user = await GetUserAsync();

            if (!await userManager.CheckPasswordAsync(user, password))
            {
                return(BadRequest(ErrorView.SoftError("PasswordInvalid", "Password not valid")));
            }

            if (await userManager.CheckEmailInDbAsync(email, user.Id))
            {
                return(BadRequest(ErrorView.SoftError("EmailAlreadyTaken", "Email already registered")));
            }

            await accountManager.SendChangeEmailConfirmationMessageByEmailAsync(user, email);

            return(Ok());
        }
        public async Task <User> LoginAsync(string nameOrEmail, string password)
        {
            User user = await userManager.FindUserByNameOrEmailAsync(nameOrEmail);

            if (user == null || !await userManager.CheckPasswordAsync(user, password))
            {
                throw new SunViewException(ErrorView.SoftError("UsernamePasswordInvalid",
                                                               "The username or password is invalid."));
            }

            if (!await userManager.IsEmailConfirmedAsync(user))
            {
                throw new SunViewException(ErrorView.SoftError("EmailNotConfirmed",
                                                               "You must have a confirmed email to log in."));
            }

            if (await userManager.IsUserInRoleAsync(user.Id, RoleNames.Banned))
            {
                throw new SunViewException(new ErrorView("UserBanned", "User is banned", ErrorType.System));
            }

            return(user);
        }
Example #5
0
        public async Task <UserServiceResult> LoginAsync(string nameOrEmail, string password)
        {
            User user = await userManager.FindUserByNameOrEmailAsync(nameOrEmail);

            if (user == null || !await userManager.CheckPasswordAsync(user, password))
            {
                return(UserServiceResult.BadResult(
                           ErrorView.SoftError("UsernamePasswordInvalid", "The username or password is invalid.")));
            }

            if (!await userManager.IsEmailConfirmedAsync(user))
            {
                return(UserServiceResult.BadResult(
                           ErrorView.SoftError("EmailNotConfirmed", "You must have a confirmed email to log in.")));
            }

            if (await userManager.IsUserInRoleAsync(user.Id, RoleNames.Banned))
            {
                return(UserServiceResult.BadResult(new ErrorView("UserBanned", "User is banned")));
            }

            return(UserServiceResult.OkResult(user));
        }