public async Task <ActionResult> PostAssignRolesToUser([FromBody] RoleUserAssignmentRequest roleAssignmentRequest)
        {
            var dbTransaction = await _dataContext.Database.BeginTransactionAsync();

            var result = await _roleManagementRepository.AssignRolesToUser(roleAssignmentRequest);

            if (result.StatusCode == Utils.Success)
            {
                await dbTransaction.CommitAsync();

                return(StatusCode(StatusCodes.Status200OK, result));
            }
            else
            {
                await dbTransaction.RollbackAsync();

                return(StatusCode(StatusCodes.Status400BadRequest, result));
            }
        }
        public async Task <ReturnResponse> CreateCustomer(CustomerRequest customerRequest)
        {
            if (customerRequest == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            if (string.IsNullOrWhiteSpace(customerRequest.EmailAddress) || string.IsNullOrWhiteSpace(customerRequest.FullName) || string.IsNullOrWhiteSpace(customerRequest.PhoneNumber) || string.IsNullOrWhiteSpace(customerRequest.Address) || string.IsNullOrWhiteSpace(customerRequest.Password))
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            var roleToFind = await _roleManager.FindByNameAsync(Utils.CustomerRole);

            if (roleToFind == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotFound,
                    StatusMessage = "Role Not Found"
                });
            }

            if (await _authRepository.UserEmailExists(customerRequest.EmailAddress))
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectExists,
                    StatusMessage = Utils.StatusMessageObjectExists
                });
            }

            var customer = new Customer()
            {
                EmailAddress = customerRequest.EmailAddress,
                FullName     = customerRequest.FullName,
                PhoneNumber  = customerRequest.PhoneNumber,
                Address      = customerRequest.Address
            };

            var user = new User()
            {
                UserName = customerRequest.EmailAddress,
                Email    = customerRequest.EmailAddress,
                //UserTypeId = customer.CustomerId,
                UserType = Utils.Customer,
                Customer = customer
            };

            var result = await _userManager.CreateAsync(user, customerRequest.Password);

            if (result.Succeeded)
            {
                //UPDATE USERTYPEID IN USER TABLE
                user.UserTypeId = customer.CustomerId;
                var updateResult = await _userManager.UpdateAsync(user);

                if (!updateResult.Succeeded)
                {
                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.NotSucceeded,
                        StatusMessage = Utils.StatusMessageNotSucceeded
                    });
                }

                //ASSIGN CUSTOMER ROLE TO USER (CUSTOMER)
                var assignmentResult = await _roleManagementRepository.AssignRolesToUser(new RoleUserAssignmentRequest()
                {
                    Users = new List <int>()
                    {
                        user.Id
                    },
                    Roles = new List <int>()
                    {
                        roleToFind.Id
                    }
                });

                if (assignmentResult.StatusCode == Utils.Success)
                {
                    //SEND MAIL TO CUSTOMER TO CONFIRM EMAIL
                    var userTokenVal = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    string hashedEmail           = _authRepository.GetHashedEmail(user.Email);
                    var    fullToken             = userTokenVal + "#" + hashedEmail;
                    var    emailVerificationLink = _authRepository.GetUserEmailVerificationLink(fullToken);
                    if (emailVerificationLink == null)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.ObjectNull,
                            StatusMessage = Utils.StatusMessageObjectNull
                        });
                    }

                    var    emailMessage1 = "Please click the button below to complete your registration and activate you account.";
                    var    emailMessage2 = "";
                    string emailBody     = _globalRepository.GetMailBodyTemplate(customer.FullName, "", emailVerificationLink, emailMessage1, emailMessage2, "activation.html");
                    var    emailSubject  = "CONFIRM YOUR EMAIL ADDRESS";
                    //SEND MAIL TO CUSTOMER TO VERIFY EMAIL
                    MailModel mailObj  = new MailModel(_configuration.GetValue <string>("MercuryMartEmailAddress"), _configuration.GetValue <string>("MercuryMartEmailName"), customer.EmailAddress, emailSubject, emailBody);
                    var       response = await _mailRepository.SendMail(mailObj);

                    if (!response.StatusCode.Equals(HttpStatusCode.Accepted))
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.MailFailure,
                            StatusMessage = Utils.StatusMessageMailFailure
                        });
                    }

                    var customerToReturn = await GetCustomers(customer.CustomerId);

                    if (customerToReturn.StatusCode != Utils.Success)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.NotSucceeded,
                            StatusMessage = "Error Occured while Fetching Customer Information"
                        });
                    }

                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.Success,
                        StatusMessage = "Registration Successful!!!",
                        ObjectValue = (Customer)customerToReturn.ObjectValue
                    });
                }

                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotSucceeded,
                    StatusMessage = Utils.StatusMessageNotSucceeded
                });
            }

            return(new ReturnResponse()
            {
                StatusCode = Utils.NotSucceeded,
                StatusMessage = Utils.StatusMessageNotSucceeded
            });
        }
예제 #3
0
        public async Task <ReturnResponse> CreateAdministrator(AdministratorRequest administratorRequest)
        {
            //REGISTER Administrator
            if (administratorRequest == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            if (string.IsNullOrWhiteSpace(administratorRequest.EmailAddress) || string.IsNullOrWhiteSpace(administratorRequest.FullName))
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            var roleToFind = await _roleManager.FindByIdAsync(Convert.ToString(administratorRequest.RoleId));

            if (roleToFind == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotFound,
                    StatusMessage = "Role Not Found"
                });
            }

            if (await _authRepository.UserEmailExists(administratorRequest.EmailAddress))
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectExists,
                    StatusMessage = "Email already exist(s)!"
                });
            }

            var Administrator = new Administrator
            {
                EmailAddress = administratorRequest.EmailAddress,
                FullName     = administratorRequest.FullName
            };

            var user = new User
            {
                UserName = administratorRequest.EmailAddress,
                Email    = administratorRequest.EmailAddress,
                //UserTypeId = Administrator.AdministratorId,
                UserType      = Utils.Administrator,
                Administrator = Administrator
            };

            var password = _helper.RandomPassword();
            var result   = await _userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                //UPDATE USERTYPEID IN USER TABLE
                user.UserTypeId = Administrator.AdministratorId;
                var updateResult = await _userManager.UpdateAsync(user);

                if (!updateResult.Succeeded)
                {
                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.NotSucceeded,
                        StatusMessage = Utils.StatusMessageNotSucceeded
                    });
                }

                //ASSIGN ROLES FROM THE REQUEST DTO TO USER
                var assignmentResult = await _roleManagementRepository.AssignRolesToUser(new RoleUserAssignmentRequest()
                {
                    Users = new List <int>()
                    {
                        user.Id
                    },
                    Roles = new List <int>()
                    {
                        administratorRequest.RoleId
                    }
                });

                if (assignmentResult.StatusCode == Utils.Success)
                {
                    var userTokenVal = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    string hashedEmail           = _authRepository.GetHashedEmail(user.Email);
                    string fullToken             = userTokenVal + "#" + hashedEmail;
                    var    emailVerificationLink = _authRepository.GetUserEmailVerificationLink(fullToken);
                    if (emailVerificationLink == null)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.ObjectNull,
                            StatusMessage = "Could not generate Email Verification Link"
                        });
                    }

                    var emailMessage1 = $"Your default password is {password}. It is recommended that you change this password after Confirming your Account and Logging In.";
                    var boldedPasswordEmailMessage = Regex.Replace(emailMessage1, password, @"<b>$0</b>", RegexOptions.IgnoreCase);
                    var emailMessage2 = "Please click the button below to complete your registration and activate your account.";
                    var emailBody     = _globalRepository.GetMailBodyTemplate(Administrator.FullName, "", emailVerificationLink, boldedPasswordEmailMessage, emailMessage2, "activation.html");
                    var emailSubject  = "CONFIRM YOUR EMAIL ADDRESS";
                    //SEND MAIL TO ADMINISTRATOR TO VERIFY EMAIL
                    MailModel mailObj  = new MailModel(_configuration.GetValue <string>("MercuryMartEmailAddress"), _configuration.GetValue <string>("MercuryMartEmailName"), Administrator.EmailAddress, emailSubject, emailBody);
                    var       response = await _mailRepository.SendMail(mailObj);

                    if (!response.StatusCode.Equals(HttpStatusCode.Accepted))
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.MailFailure,
                            StatusMessage = "Error Occured while sending Mail to Administrator"
                        });
                    }

                    var administratorToReturn = await GetAdministrators(Administrator.AdministratorId);

                    if (administratorToReturn.StatusCode != Utils.Success)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.NotSucceeded,
                            StatusMessage = "Error Occured while Fetching Administrator Information"
                        });
                    }

                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.Success,
                        ObjectValue = (Administrator)administratorToReturn.ObjectValue,
                        StatusMessage = "Administrator Created Successfully!!!"
                    });
                }

                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotSucceeded,
                    StatusMessage = "Error Occured while saving Administrator Information"
                });
            }

            return(new ReturnResponse()
            {
                StatusCode = Utils.NotSucceeded,
                StatusMessage = "Error Occured while saving Administrator Information"
            });
        }