Exemple #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            if (GlobalContext.WebSite.AllowRegistration == false)
            {
                TempData["ErrorMessage"] = "Registration is not allowed.";
                return(Redirect("Home/Error"));
            }

            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new NccUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var subscriber = _nccPermissionService.Get(GlobalContext.WebSite.NewUserRole);
                    user.Permissions.Add(new NccUserPermission()
                    {
                        Permission = subscriber, User = user
                    });
                    UserService.Update(user);
                    _logger.LogInformation("User created a new account with password.");

                    var rsp = FireEvent(UserActivity.Type.Registered, model.Email, user, new List <string>()
                    {
                        GlobalContext.WebSite.NewUserRole
                    }, "");
                    if (rsp != null)
                    {
                        returnUrl = rsp.ReturnUrl;
                    }

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme);

                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

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

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

                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(NccUrlHelper.AddLanguageToUrl(CurrentLanguage, "/CmsHome")));
                    }

                    return(Redirect(returnUrl));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #2
0
        public ActionResult CreateEditRoles(long roleId = 0)
        {
            var model         = new PermissionViewModel();
            var activeModules = GlobalContext.GetActiveModules();

            ViewBag.Modules = activeModules;

            if (roleId > 0)
            {
                var permission = _nccPermissionService.Get(roleId);
                if (permission != null)
                {
                    model = GetPermissionViewModel(permission);
                }
                else
                {
                    ViewBag.InfoMessage = "Role not found.";
                }
            }
            return(View(model));
        }
        public ActionResult CreateEdit(UserViewModel user, string SendEmail)
        {
            bool   isSuccess     = false;
            string returnMessage = "User Creation failed";

            if (user.Id > 0 && !string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.FullName) && !string.IsNullOrEmpty(user.Mobile))
            {
                var oldUser = _nccUserService.GetNccUser(user.Id);

                oldUser.FullName = user.FullName;
                oldUser.Email    = user.Email;
                oldUser.Mobile   = user.Mobile;

                oldUser.ExtraDenies.RemoveAll(x => x.ExtraDenyUserId == user.Id);
                oldUser.ExtraPermissions.RemoveAll(x => x.ExtraAllowUserId == user.Id);

                var allowedAdminMenuItems = user.AllowModules.Select(x => new { ModuleId = x.ModuleName, Items = x.AdminMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) });

                var allowedWebSiteMenuItems = user.AllowModules.Select(x => new { ModuleId = x.ModuleName, Items = x.SiteMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }).ToList();

                foreach (var moduleMenu in allowedAdminMenuItems)
                {
                    foreach (var menuItem in moduleMenu.Items)
                    {
                        oldUser.ExtraPermissions.Add(new NccPermissionDetails()
                        {
                            Action           = menuItem.Action,
                            AllowUser        = oldUser,
                            Controller       = menuItem.Controller,
                            ExtraAllowUserId = oldUser.Id,
                            ModuleName       = moduleMenu.ModuleId
                        });
                    }
                }

                foreach (var moduleMenu in allowedWebSiteMenuItems)
                {
                    foreach (var menuItem in moduleMenu.Items)
                    {
                        oldUser.ExtraPermissions.Add(new NccPermissionDetails()
                        {
                            Action           = menuItem.Action,
                            AllowUser        = oldUser,
                            Controller       = menuItem.Controller,
                            ExtraAllowUserId = oldUser.Id,
                            ModuleName       = moduleMenu.ModuleId
                        });
                    }
                }

                var deniedAdminMenuItems = user.DenyModules.Select(x => new { ModuleId = x.ModuleName, Items = x.AdminMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) });

                var deniedWebSiteMenuItems = user.DenyModules.Select(x => new { ModuleId = x.ModuleName, Items = x.SiteMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }).ToList();

                foreach (var moduleMenu in deniedAdminMenuItems)
                {
                    foreach (var menuItem in moduleMenu.Items)
                    {
                        oldUser.ExtraDenies.Add(new NccPermissionDetails()
                        {
                            Action          = menuItem.Action,
                            DenyUser        = oldUser,
                            Controller      = menuItem.Controller,
                            ExtraDenyUserId = oldUser.Id,
                            ModuleName      = moduleMenu.ModuleId
                        });
                    }
                }

                foreach (var moduleMenu in deniedWebSiteMenuItems)
                {
                    foreach (var menuItem in moduleMenu.Items)
                    {
                        oldUser.ExtraDenies.Add(new NccPermissionDetails()
                        {
                            Action          = menuItem.Action,
                            DenyUser        = oldUser,
                            Controller      = menuItem.Controller,
                            ExtraDenyUserId = oldUser.Id,
                            ModuleName      = moduleMenu.ModuleId
                        });
                    }
                }

                _nccUserService.Update(oldUser);

                if (user.IsSuperAdmin)
                {
                    NccUser userTemp = _userManager.FindByNameAsync(oldUser.UserName).Result;
                    var     result   = _userManager.AddToRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result;
                    if (result.Succeeded)
                    {
                        isSuccess     = true;
                        returnMessage = " User SuperAdmin Role update successful.";
                    }
                    else
                    {
                        isSuccess     = false;
                        returnMessage = result.Errors?.FirstOrDefault()?.Description;
                    }
                }
                else
                {
                    NccUser userTemp = _userManager.FindByNameAsync(oldUser.UserName).Result;
                    var     result   = _userManager.RemoveFromRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result;
                    if (result.Succeeded)
                    {
                        isSuccess     = true;
                        returnMessage = " User SuperAdmin Role remove successful.";
                    }
                    else
                    {
                        isSuccess     = false;
                        returnMessage = result.Errors?.FirstOrDefault()?.Description;
                    }
                }

                isSuccess = true;
                GlobalContext.GlobalCache.SetNccUser(oldUser);
                //return RedirectToAction("Index");
            }
            else if (ModelState.IsValid)
            {
                if (user.Password == user.ConfirmPassword)
                {
                    var nccUser = new NccUser()
                    {
                        Email = user.Email, FullName = user.FullName, UserName = user.UserName, Mobile = user.Mobile, Status = EntityStatus.Active
                    };
                    var result = _userManager.CreateAsync(nccUser, user.Password).Result;

                    var createdUser = _userManager.FindByNameAsync(user.UserName).Result;
                    if (createdUser != null)
                    {
                        if (user.IsSuperAdmin)
                        {
                            NccUser userTemp = _userManager.FindByNameAsync(createdUser.UserName).Result;
                            var     temp     = _userManager.AddToRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result;
                            isSuccess     = true;
                            returnMessage = "User created successfully.";
                        }
                        else
                        {
                            foreach (var item in user.Roles)
                            {
                                var permission = _nccPermissionService.Get(item);
                                createdUser.Permissions.Add(new NccUserPermission()
                                {
                                    Permission = permission, User = createdUser
                                });
                            }

                            createdUser.ExtraPermissions = GetSelectedPermissionDetails(user.AllowModules, createdUser, true);
                            createdUser.ExtraDenies      = GetSelectedPermissionDetails(user.DenyModules, createdUser, false);

                            var upResult = _userManager.UpdateAsync(createdUser).Result;
                            if (upResult.Succeeded == false)
                            {
                                returnMessage = "User role assign failed.";
                            }
                            else
                            {
                                GlobalContext.GlobalCache.SetNccUser(createdUser);
                                isSuccess     = true;
                                returnMessage = "User created successfully.";
                            }
                        }
                    }
                }
                else
                {
                    returnMessage = "Password does not match.";
                }
            }
            else
            {
                returnMessage = "Please enter all required fields.";
                ModelState.AddModelError("", "Please enter all required fields.");
            }

            if (isSuccess)
            {
                ShowMessage(returnMessage, MessageType.Success, false, true);
                return(RedirectToAction("CreateEdit"));
            }

            var activeModules = GlobalContext.GetActiveModules();

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

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

            ShowMessage(returnMessage, MessageType.Error);
            return(View("CreateEdit", user));
        }