Ejemplo n.º 1
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            UserRoleManager _userRoleManager = new UserRoleManager();
            AppRoleManager  _roleManager     = new AppRoleManager();

            var role     = _roleManager.GetAuthorUserRoleId();
            var userRole = _userRoleManager.Find(x => x.AppUserId == CurrentSession.User.Id && x.AppRoleId == role);

            if (userRole == null)
            {
                filterContext.Result = new RedirectResult("/Error/AccessDenied");
            }
        }
Ejemplo n.º 2
0
        public ActionResult EditUser(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                Guid roleId;
                switch (model.RoleId)
                {
                case 1:
                    roleId = _roleManager.GetUserRoleId();
                    break;

                case 2:
                    roleId = _roleManager.GetAuthorUserRoleId();
                    break;

                case 3:
                    roleId = _roleManager.GetAdminRoleId();
                    break;

                default:
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }


                UserRole userRole = new UserRole()
                {
                    AppUserId = model.UserId,
                    AppRoleId = roleId
                };
                BusinessLayerResult <UserRole> res = _userRoleManager.Insert(userRole);
                if (res.Errors.Count > 0)
                {
                    // başarısız
                    res.Errors.ForEach(x => ModelState.AddModelError("", x.Message));
                }
                else
                {
                    // başarılı
                    return(RedirectToAction("EditUser", "SystemTool", new { @userId = model.UserId }));
                }
            }

            var user = _userManager.Find(x => x.Id == model.UserId);

            model.UserRoles = user.UserRoles;

            List <SelectListItem> RoleListId = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Kullanıcı", Value = "1"
                },
                new SelectListItem()
                {
                    Text = "Yazar", Value = "2"
                },
                new SelectListItem()
                {
                    Text = "Yönetici", Value = "3"
                }
            };

            ViewBag.RoleId = new SelectList(RoleListId, "Value", "Text", model.RoleId);
            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Login(LoginViewModel model)
        {
            // giriş kontrolü ve yönlendirme
            // sessiona kullanıcı bilgi saklama
            if (ModelState.IsValid)
            {
                BusinessLayerResult <AppUser> res = _userManager.LoginUser(model);
                if (res.Errors.Count > 0)
                {
                    res.Errors.ForEach(x => ModelState.AddModelError("", x.Message));
                    return(View(model));
                }
                else
                {
                    // role göre yönlendirme
                    var admin  = _roleManager.GetAdminRoleId();
                    var author = _roleManager.GetAuthorUserRoleId();
                    var user   = _roleManager.GetUserRoleId();

                    var userRole = _userRoleManager.Find(x => x.AppUserId == res.Result.Id && x.AppRoleId == admin);

                    if (userRole != null)
                    {
                        LastVisit lv = new LastVisit()
                        {
                            AppUserId = res.Result.Id,
                            DateTime  = DateTime.Now,
                            IpAddress = GetClientIp()
                        };

                        int dbres = _lastVisitManager.Insert(lv);

                        CurrentSession.Set <AppUser>("login", res.Result);
                        return(RedirectToAction("Index", "Home", new { area = "Admin" }));
                    }

                    userRole = _userRoleManager.Find(x => x.AppUserId == res.Result.Id && x.AppRoleId == author);

                    if (userRole != null)
                    {
                        LastVisit lv = new LastVisit()
                        {
                            AppUserId = res.Result.Id,
                            DateTime  = DateTime.Now,
                            IpAddress = GetClientIp()
                        };

                        int dbres = _lastVisitManager.Insert(lv);

                        CurrentSession.Set <AppUser>("login", res.Result);
                        return(RedirectToAction("Index", "Home", new { area = "Author" }));
                    }

                    userRole = _userRoleManager.Find(x => x.AppUserId == res.Result.Id && x.AppRoleId == user);

                    if (userRole != null)
                    {
                        LastVisit lv = new LastVisit()
                        {
                            AppUserId = res.Result.Id,
                            DateTime  = DateTime.Now,
                            IpAddress = GetClientIp()
                        };

                        int dbres = _lastVisitManager.Insert(lv);

                        CurrentSession.Set <AppUser>("login", res.Result);
                        return(RedirectToAction("Index", "Home", new { area = "User" }));
                    }

                    ModelState.AddModelError("", "Giriş yapılamıyor");
                }
            }
            return(View(model));
        }