Пример #1
0
        public async Task <ServiceResponseResult> Register(RegisterModel model)
        {
            Logger.WriteInformation("Registering.");
            if (await _context.User.AnyAsync(x => x.Email == model.Email.ToLower()) || await _context.ChangeEmailRequest.AnyAsync(x => x.Email == model.Email.ToLower()))
            {
                return(new ServiceResponseResult
                {
                    StatusCode = System.Net.HttpStatusCode.BadRequest,
                    Result = new { Message = "Email already exists in the system." }
                });
            }

            var user = UserMapper.ToUser(model, _appSettings.Secret);

            _context.User.Add(user);
            await _context.SaveChangesAsync();

            var emailData = await _emailService.ConstructEmailVerification(user.VerificationToken);

            await _emailSender.SendMailViaSmtpClientAsync(new string[] { user.Email }, new string[] { }, new string[] { }, emailData);

            return(new ServiceResponseResult
            {
                StatusCode = System.Net.HttpStatusCode.OK
            });
        }
Пример #2
0
        public async Task <ServiceResponseResult> UpdateAdminProfile(AdminProfileUpdateReqModel model, long userId, long locationId)
        {
            Logger.WriteInformation("Updating admin profile data.");
            var user = await _context.User.FirstOrDefaultAsync(x => x.Id == userId);

            var token = Guid.NewGuid().ToString();
            var now   = DateTime.UtcNow;

            if (!string.IsNullOrEmpty(model.Email))
            {
                if (await _context.User.AnyAsync(x => x.Email == model.Email.ToLower()) || await _context.ChangeEmailRequest.AnyAsync(x => x.Email == model.Email.ToLower()))
                {
                    return(new ServiceResponseResult
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        Result = new { Message = "An user with the email already exists." },
                    });
                }


                var emailChange = new ChangeEmailRequest
                {
                    Email                   = model.Email,
                    RequestedOn             = DateTime.UtcNow,
                    UserId                  = userId,
                    VerificationToken       = token,
                    VerificationTokenExpiry = now.AddHours(24)
                };

                _context.ChangeEmailRequest.Add(emailChange);

                var emailData = await _emailService.ConstructEmailVerification(user.VerificationToken);

                await _emailSender.SendMailViaSmtpClientAsync(new string[] { model.Email }, new string[] { }, new string[] { }, emailData);
            }

            if (!string.IsNullOrEmpty(model.Name))
            {
                user = user.UpdateName(model.Name);
            }

            user = user.UpdateVerificationToken(token)
                   .UpdateVerificationTokenExpiry(now.AddHours(24))
                   .UpdateLastUpdatedOn(DateTime.UtcNow)
                   .UpdateLastUpdatedBy(userId);

            _context.User.Update(user);
            await _context.SaveChangesAsync();

            Logger.WriteInformation("Updating admin profile data completed.");
            return(await GetAdminProfile(locationId, userId));
        }
        public async Task <ServiceResponseResult> UpdatedAdmin(long locationId, long userId, long adminUserId, AdministratorUpdateReqModel model)
        {
            Logger.WriteInformation("Getiing admin activity data.");
            var userLoc = await _context.UserLocation.FirstOrDefaultAsync(x => x.UserId == adminUserId && x.LocationId == locationId);

            if (userLoc == null)
            {
                return(new ServiceResponseResult
                {
                    Result = new { Message = $"User does not exists in this location {locationId}" },
                    StatusCode = System.Net.HttpStatusCode.NotFound
                });
            }

            var user = await _context.User.FindAsync(adminUserId);

            if (!string.IsNullOrEmpty(model.Email) && model.Email != user.Email)
            {
                var doesEmailExists = await _context.ChangeEmailRequest.AnyAsync(x => x.Email == model.Email.ToLower()) || await _context.User.AnyAsync(x => x.Email == model.Email.ToLower());

                if (doesEmailExists)
                {
                    return(new ServiceResponseResult
                    {
                        Result = new { Message = $"Could not update the email as it is already exists" },
                        StatusCode = System.Net.HttpStatusCode.BadRequest
                    });
                }


                var emailChange = new ChangeEmailRequest
                {
                    Email                   = model.Email,
                    RequestedOn             = DateTime.UtcNow,
                    UserId                  = adminUserId,
                    VerificationToken       = Guid.NewGuid().ToString(),
                    VerificationTokenExpiry = DateTime.UtcNow.AddHours(24)
                };
                _context.ChangeEmailRequest.Add(emailChange);

                var emailData = await _emailService.ConstructEmailVerification(emailChange.VerificationToken);

                await _emailSender.SendMailViaSmtpClientAsync(new string[] { emailChange.Email }, new string[] { }, new string[] { }, emailData);
            }

            if (!string.IsNullOrEmpty(model.Name))
            {
                user = user.UpdateName(model.Name).UpdateLastUpdatedBy(userId)
                       .UpdateLastUpdatedOn(DateTime.UtcNow);
            }

            _context.Update <User>(user);

            if (model.State.HasValue)
            {
                if (model.State.Value == AdministratorState.Disabled && string.IsNullOrEmpty(model.DisabledReason))
                {
                    return(new ServiceResponseResult
                    {
                        Result = new { Message = $"DisabledReason is required for disable state." },
                        StatusCode = System.Net.HttpStatusCode.BadRequest
                    });
                }

                userLoc.State         = (int)model.State.Value;
                userLoc.LastUpdatedBy = userId;
                userLoc.LastUpdatedOn = DateTime.UtcNow;
                _context.Update(userLoc);
            }

            var response = new AdministratorResult(user, userLoc);

            if (model.Status != null)
            {
                var status = await _context.UserStatus.FirstOrDefaultAsync(x => x.Id == model.Status.Id && x.LocationId == locationId);

                if (status != null)
                {
                    userLoc.StatusId      = status.Id;
                    userLoc.LastUpdatedBy = userId;
                    userLoc.LastUpdatedOn = DateTime.UtcNow;
                    _context.Update(userLoc);
                    response.AddStatus(status);
                }
            }

            var userPermission = await _context.UserPermission.FirstOrDefaultAsync(x => x.UserLocationId == userLoc.Id);

            if (userPermission == null)
            {
                userPermission = UserPermissionMapper.ToUserPermission(model.Permissions)
                                 .UpdateLastUpdatedOn(DateTime.UtcNow)
                                 .UpdateUserLocationId(userLoc.Id);
                _context.Add <UserPermission>(userPermission);
            }
            else
            {
                userPermission = UserPermissionMapper.ToUserPermission(model.Permissions);
                userPermission = userPermission.UpdateLastUpdatedBy(userId)
                                 .UpdateId(userPermission.Id)
                                 .UpdateLastUpdatedOn(DateTime.UtcNow)
                                 .UpdateUserLocationId(userLoc.Id);

                _context.Update <UserPermission>(userPermission);
            }

            response.AddPermission(userPermission);

            var key = await _context.KeyHolder.FirstOrDefaultAsync(x => x.KeySerialNumber == model.KeySerialNumber);

            var userKeyMapping = await _context.UserKeyMapping
                                 .FirstOrDefaultAsync(x => x.UserId == adminUserId && x.LocationId == locationId && x.KeySerialNumber == model.KeySerialNumber);

            if (key != null)
            {
                if (userKeyMapping == null)
                {
                    userKeyMapping = new UserKeyMapping
                    {
                        AppliedOn       = DateTime.UtcNow,
                        KeySerialNumber = key.KeySerialNumber,
                        LocationId      = locationId,
                        UserId          = user.Id
                    };

                    userLoc.UpdateIsToolKitEnabled(true);
                    _context.Update(userLoc);

                    _context.Add <UserKeyMapping>(userKeyMapping);
                }
                response.AddToolkit(key);
            }
            await _context.SaveChangesAsync();

            Logger.WriteInformation("Updating admin activity data completed.");

            return(new ServiceResponseResult
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Result = response
            });
        }