Beispiel #1
0
        public ActionResult CreateEdit(string userName = "")
        {
            var activeModules = GlobalContext.GetActiveModules();

            ViewBag.Modules = activeModules;
            var permissions = _nccPermissionService.LoadAll();

            ViewBag.Roles = new SelectList(permissions, "Id", "Name");

            var user = new UserViewModel();

            if (!string.IsNullOrEmpty(userName))
            {
                NccUser nccUser = _nccUserService.GetByUserName(userName);
                user          = new UserViewModel(nccUser);
                ViewBag.Roles = new SelectList(permissions, "Id", "Name", nccUser.Permissions.Select(x => x.PermissionId).ToArray());
            }
            return(View(user));
        }
Beispiel #2
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            if (string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = NccUrlHelper.AddLanguageToUrl(CurrentLanguage, "/CmsHome");
            }

            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    var user  = _nccUserService.GetByUserName(model.Email);
                    var roles = _nccPermissionService.LoadAll();
                    var rsp   = FireEvent(UserActivity.Type.Logedin, model.Email, user, roles.Select(x => x.Name).ToList(), returnUrl);

                    if (rsp != null)
                    {
                        returnUrl = rsp.ReturnUrl;
                    }

                    foreach (var item in user.Roles)
                    {
                        if (item.Role.Name == NccCmsRoles.SuperAdmin)
                        {
                            return(RedirectToLocal("/Admin"));
                        }
                    }

                    var startups = _startupService.LoadAll();
                    foreach (var item in startups)
                    {
                        if (user.Permissions.Where(x => x.Permission.Name == item.Permission.Name).Count() > 0)
                        {
                            return(Redirect(item.StartupUrl));
                        }
                    }

                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToAction(nameof(Lockout)));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }