Esempio n. 1
0
        public async Task <DocumentUpdateResultDto> UpdateAsync(UserDto userDto, bool isBulkImport = false)
        {
            ValidateUser(userDto);
            await CheckForDuplicatesAsync(userDto.Email, userDto.AlternateId, userDto.Id);

            var user = await _systemDataContext.GetDocumentAsync <User>(userDto.Id);

            //Validate access
            await CheckAccessToUserAsync(user, isBulkImport?BulkData.PermissionKey : Permission_Update);

            var previousRoles       = user.GroupRoles;
            var previousPIN         = user.PIN;
            var previousFirstName   = user.FirstName;
            var previousLastName    = user.LastName;
            var previousAlternateId = user.AlternateId;

            await ValidatePINAsync(userDto.PIN, user.GroupRoles.Select(cr => cr.Group).ToList());

            var roleAssignments = await GetGroupRolesToUpdateUserAsync(user.GroupRoles, userDto.GroupRoles, isBulkImport);

            ValidateUserRoles(roleAssignments);

            user.FirstName = userDto.FirstName;
            user.LastName  = userDto.LastName;
            user.FullName  = string.Format("{0} {1}", userDto.FirstName, userDto.LastName);
            user.Culture   = userDto.Culture;
            if (!string.IsNullOrEmpty(userDto.PIN))
            {
                user.PIN = CryptographicProvider.GenerateUserPINHash(userDto.PIN);
            }

            user.TimeZone     = userDto.TimeZone;
            user.MobileNumber = new MobileNumber
            {
                IddCode = userDto.MobileNumber?.IddCode,
                Number  = userDto.MobileNumber?.Number
            };
            user.AlternateId      = userDto.AlternateId;
            user.Email            = userDto.Email;
            user.AccessExpiryDate = userDto.AccessExpiryDate;
            user.IsDeleted        = userDto.IsDeleted;
            user.GroupRoles       = roleAssignments;
            user.ETag             = userDto.ETag;
            user.ImageBlobId      = userDto.ImageBlobId;
            if (userDto.Purposes != null && userDto.Purposes.Count > 0)
            {
                user.Purposes = new List <string>();
                user.Purposes.AddRange(userDto.Purposes);
            }

            var result = await _systemDataContext.ReplaceDocumentAsync(user);

            var addedRoles   = user.GroupRoles.Where(cr => !previousRoles.Any(r => r.Group == cr.Group && r.Role == cr.Role));
            var deletedRoles = previousRoles.Where(cr => !user.GroupRoles.Any(r => r.Group == cr.Group && r.Role == cr.Role));

            bool normalizationRequired =
                userDto.PIN != previousPIN ||
                userDto.FirstName != previousFirstName ||
                userDto.LastName != previousLastName ||
                userDto.AlternateId != previousAlternateId ||
                addedRoles.Any() ||
                deletedRoles.Any();

            if (normalizationRequired)
            {
                var userUpdateMessageDto = new UserUpdateMessageDto
                {
                    UserId     = user.Id,
                    GroupRoles = new List <UserGroupRoleDto>()
                };

                if (userDto.PIN != previousPIN || userDto.FirstName != previousFirstName || userDto.LastName != previousLastName || userDto.AlternateId != previousAlternateId)
                {
                    userUpdateMessageDto.GroupRoles.AddRange(user.GroupRoles.Select(cr => new UserGroupRoleDto {
                        GroupId = cr.Group, Role = cr.Role
                    }));
                }

                else if (addedRoles.Any())
                {
                    userUpdateMessageDto.GroupRoles.AddRange(addedRoles.Select(cr => new UserGroupRoleDto {
                        GroupId = cr.Group, Role = cr.Role
                    }));
                }

                if (deletedRoles.Any())
                {
                    userUpdateMessageDto.GroupRoles.AddRange(deletedRoles.Select(cr => new UserGroupRoleDto {
                        GroupId = cr.Group, Role = cr.Role
                    }));
                }

                await _platformServiceBusService.TriggerPodAccessDefinitionGenerationAsync(userUpdateMessageDto);
            }

            return(result);
        }
Esempio n. 2
0
        public async Task <DocumentUpdateResultDto> CreateAsync(UserDto userDto, bool isBulkImport = false)
        {
            var result = new DocumentUpdateResultDto();

            try
            {
                ValidateUser(userDto);
                await CheckForDuplicatesAsync(userDto.Email, userDto.AlternateId);
                await ValidatePINAsync(userDto.PIN, userDto.GroupRoles.Select(cr => cr.GroupId).ToList());

                var roleAssignments = await CreateUserGroupRolesFromDtoAsync(userDto.GroupRoles,
                                                                             isBulkImport?BulkData.PermissionKey : Permission_New);

                ValidateUserRoles(roleAssignments);

                var tag = userDto.Tag?.Trim();

                if (string.IsNullOrEmpty(tag))
                {
                    tag = await GetUniqueTagAsync(userDto.FirstName);
                }
                else
                {
                    await ValidateTagUniquenessAsync(userDto.Tag);
                }

                var user = new User
                {
                    FirstName          = userDto.FirstName,
                    LastName           = userDto.LastName,
                    FullName           = string.Format("{0} {1}", userDto.FirstName, userDto.LastName),
                    NormalizedFullName = string.Format("{0} {1}", userDto.FirstName, userDto.LastName).ToUpper(),
                    Culture            = userDto.Culture,
                    PIN          = !string.IsNullOrWhiteSpace(userDto.PIN) ? CryptographicProvider.GenerateUserPINHash(userDto.PIN) : null,
                    TimeZone     = userDto.TimeZone,
                    MobileNumber = new MobileNumber
                    {
                        IddCode = userDto.MobileNumber?.IddCode,
                        Number  = userDto.MobileNumber?.Number
                    },
                    AlternateId        = userDto.AlternateId,
                    Email              = userDto.Email,
                    UserName           = userDto.Email,
                    NormalizedUserName = userDto.Email.ToUpper(),
                    NormalizedEmail    = userDto.Email.ToUpper(),
                    AccessExpiryDate   = userDto.AccessExpiryDate,
                    GroupRoles         = roleAssignments,
                    UserLocation       = new UserLocation
                    {
                        Country = userDto.UserLocation?.Country,
                        State   = userDto.UserLocation?.State,
                        City    = userDto.UserLocation?.City
                    },
                    Tag          = tag,
                    ImageBlobId  = userDto.ImageBlobId,
                    PasswordHash = userDto.PasswordHash
                };
                if (userDto.Purposes != null && userDto.Purposes.Count > 0)
                {
                    user.Purposes = new List <string>();
                    user.Purposes.AddRange(userDto.Purposes);
                }

                await _identityService.CreateUserAsync(user);

                var userUpdateMessageDto = new UserUpdateMessageDto
                {
                    UserId     = user.Id,
                    GroupRoles = userDto.GroupRoles
                };

                //await _platformServiceBusService.TriggerPodAccessDefinitionGenerationAsync(userUpdateMessageDto);
                result.IsSucceeded  = true;
                result.Id           = user.Id;
                result.ETag         = user.ETag;
                result.UpdatedById  = user.UpdatedById;
                result.UpdatedOnUtc = user.UpdatedOnUtc;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                result.IsSucceeded = false;
                result.ErrorCode   = e.Message;
            }

            return(result);
        }