Esempio n. 1
0
        public async Task <SmartHomeValidationResult> ChangePasswordAsync(ChangePasswordModel changePasswordModel)
        {
            var errors = Validate(changePasswordModel);

            if (!errors.Succeeded)
            {
                return(errors);
            }

            return(SmartHomeValidationResult.FromIdentityResult(await repository.ChangePasswordAsync(changePasswordModel)));
        }
Esempio n. 2
0
        public async Task <SmartHomeValidationResult> CreateUserAsync(CreateUserModel model)
        {
            var validationResult = await ValidateAsync(model);

            if (!validationResult.Succeeded)
            {
                return(validationResult);
            }

            return(SmartHomeValidationResult.FromIdentityResult(await repository.CreateUserAsync(model)));
        }
Esempio n. 3
0
        public async Task <SmartHomeValidationResult> CreateAsync(long sensorId, double voltage, DateTime measurementDateTime)
        {
            if (!(await sensorRepository.AnyWithBatteryPowerSourceAsync(sensorId)))
            {
                return(SmartHomeValidationResult.Failed(new SmartHomeValidation(
                                                            nameof(sensorId), $"There is no sensor with id {sensorId} with battery power source.")));
            }

            await batteryMeasurementRepository.AddAsync(sensorId, voltage, measurementDateTime);

            return(SmartHomeValidationResult.Success);
        }
Esempio n. 4
0
        private SmartHomeValidationResult Validate(ChangePasswordModel model)
        {
            var errors = new List <SmartHomeValidation>();

            if (string.Equals(model.OldPassword, model.NewPassword))
            {
                errors.Add(new SmartHomeValidation(nameof(model.NewPassword),
                                                   "Old password cannot be same as the new password."));
            }

            return(SmartHomeValidationResult.Failed(errors));
        }
Esempio n. 5
0
        public static void AddValidationErrors(this ModelStateDictionary modelState,
                                               SmartHomeValidationResult smartHomeValidationResult)
        {
            if (smartHomeValidationResult.Succeeded)
            {
                return;
            }

            foreach (var validation in smartHomeValidationResult.Errors)
            {
                modelState.AddModelError(validation.Field, validation.Message);
            }
        }
Esempio n. 6
0
        public async Task <ServiceResult <long> > CreateAsync(PermissionModel model)
        {
            if (model.Id > 0)
            {
                throw new ArgumentException(nameof(model.Id), "Invalid model Id.");
            }

            if (await repository.GetByNameAsync(model.Name !) != null)
            {
                var errors = SmartHomeValidationResult.Failed(
                    new SmartHomeValidation(nameof(model.Name),
                                            $"Permission named {model.Name} already exists in the database."));

                return(new ServiceResult <long>(default, errors));
Esempio n. 7
0
        private async Task <SmartHomeValidationResult> ValidateAsync(CreateUserModel model)
        {
            var identityErrors = new List <SmartHomeValidation>();

            if (await repository.GetUserByNameAsync(model.UserName !) != null)
            {
                identityErrors.Add(new SmartHomeValidation(nameof(model.UserName),
                                                           $"User with name {model.UserName} already exists."));
            }

            if (await repository.GetUserByEmailAsync(model.Email !) != null)
            {
                identityErrors.Add(new SmartHomeValidation(nameof(model.Email),
                                                           $"User with email {model.Email} already exists."));
            }

            return(SmartHomeValidationResult.Failed(identityErrors));
        }
Esempio n. 8
0
        public async Task <SmartHomeValidationResult> AddToOrRemoveFromRolesAsync(long userId, IEnumerable <long> roleIds)
        {
            var allUserRoles = await roleRepository.GetUserRolesAsync(userId);

            var rolesToAdd = roleIds.Except(allUserRoles.Select(x => x.Id)).ToList();
            // roles that user currently has without those sent to the server
            var rolesToRemove = allUserRoles.Select(x => x.Id).Except(roleIds).ToList();

            if (rolesToAdd.Count > 0 || rolesToRemove.Count > 0)
            {
                var validationResult = SmartHomeValidationResult.FromIdentityResult(await repository.AddToRolesAsync(userId, rolesToAdd));
                validationResult = validationResult.Merge(SmartHomeValidationResult.FromIdentityResult(
                                                              await repository.RemoveFromRolesAsync(userId, rolesToRemove)));
                return(validationResult);
            }

            return(SmartHomeValidationResult.Success);
        }
Esempio n. 9
0
 public async Task <SmartHomeValidationResult> CreateRoleAsync(CreateRoleModel model)
 {
     return(SmartHomeValidationResult.FromIdentityResult(await roleRepository.CreateRoleAsync(model)));
 }
Esempio n. 10
0
 public async Task <SmartHomeValidationResult> DeleteUserAsync(long id)
 {
     return(SmartHomeValidationResult.FromIdentityResult(await repository.DeleteUserAsync(id)));
 }
Esempio n. 11
0
 public ServiceResult(TType value, SmartHomeValidationResult validationResult)
 {
     Value            = value;
     ValidationResult = validationResult;
 }
Esempio n. 12
0
 public async Task <SmartHomeValidationResult> UpdateUserAsync(UserModel model)
 {
     return(SmartHomeValidationResult.FromIdentityResult(await repository.UpdateUserAsync(model)));
 }