Exemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var listOfCountries = new List <trel_AgentCountry>();

                foreach (var item in model.IdCountries)
                {
                    var country = new trel_AgentCountry {
                        IdCountry = item
                    };
                    listOfCountries.Add(country);
                }

                var user = new User {
                    UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Countries = listOfCountries
                };

                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    logger.LogInformation("User created a new account with password.");

                    //The EmailConfirmation requires an admin approval
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    //await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    var emailTo      = string.Empty;
                    var emailSubject = "Metrics Track account request";
                    var emailBody    = string.Format(@"<p>Pending account approval for user: {0} {1} is present.&nbsp;</p>
                                                    <p>Please assign a respective team leader using the admin panel.</p>
                                                    <p><strong><sup>Metrics Track System</sup></strong></p>",
                                                     user.FirstName,
                                                     user.LastName);

                    await emailSender.SendEmailAsync(emailTo, emailSubject, emailBody);

                    await signInManager.SignInAsync(user, isPersistent : false);

                    logger.LogInformation("User created a new account with password.");

                    TempData.AddSuccessMessage("You have been successfully registered. Please wait while an administrator approves your account.");
                    return(RedirectToLocal(returnUrl));
                }

                AddErrors(result);
            }

            model.Countries = GetCountriesListItems();
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateUserAsync(UserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData.AddErrorMessage(WebConstants.InvalidModelDetails);

                return(RedirectToAction(nameof(Index)));
            }

            var user = await this.userManager.FindByIdAsync(model.User.Id);

            var userExists = user != null;

            if (!userExists)
            {
                TempData.AddErrorMessage(WebConstants.InvalidIdentityDetails);

                return(View(model));
            }

            var targetUserId  = user.Id;
            var currentUserId = HttpContext.User.Claims.Select(s => s.Value).FirstOrDefault();

            if (targetUserId.Equals(currentUserId))
            {
                TempData.AddErrorMessage(WebConstants.InvalidSelfModifications);
                return(RedirectToAction(nameof(Index)));
            }

            user.FirstName = model.User.FirstName;
            user.LastName  = model.User.LastName;
            user.Email     = model.User.Email;
            user.UserName  = model.User.Username;
            user.Sandbox   = model.User.Sandbox;

            this.users.RemoveAgentToCountryTrel(user.Id);

            if (model.IdCountries != null)
            {
                var countryList = new List <trel_AgentCountry>();

                foreach (var id in model.IdCountries)
                {
                    var trel = new trel_AgentCountry
                    {
                        IdAgent   = user.Id,
                        IdCountry = id
                    };

                    countryList.Add(trel);
                }

                user.Countries = countryList;
            }

            await this.userManager.UpdateAsync(user);

            await SetRolesAsync(user, model.IsManager, WebConstants.ManagerRole);

            await SetRolesAsync(user, model.IsAdmin, WebConstants.AdministratorRole);

            TempData.AddSuccessMessage($"{user.FirstName} {user.LastName} has been updated successfully.");

            return(RedirectToAction(nameof(Index)));
        }