Esempio n. 1
0
        public ActionResult Details(int id)
        {
            var role = _roleRepository.Get(id);

            var permissions = _rolePermissionRepository.GetAllBy(r => r.RoleId == role.Id, "Permission").Select(r => r.Permission).ToList();
            var members     = _roleMemberRepository.GetAllBy(r => r.RoleId == role.Id, "User.Person").ToList().Select(m => m.User).ToList();

            var vm = new RoleDetailsViewModel(role)
            {
                Permissions = permissions,
                Users       = members
            };

            return(View(vm));
        }
Esempio n. 2
0
        private void RecordLoginSucceeded(User user)
        {
            var userRoles   = _roleMemberRepository.GetAllBy(m => m.UserId == user.Id).Select(r => r.RoleId).ToList();
            var permissions = _rolePermissionRepository.GetAllBy(r => userRoles.Contains(r.RoleId), "Permission").Select(p => p.Permission.PermissionCode).ToList();

            var userInfo = UserInfo.GetInstance(user, permissions);

            HttpSessionWrapper.SetUserInfo(user.Code, userInfo);

            var serializeModel = new PrincipalModel(user.Code);
            var serializer     = new JavaScriptSerializer();
            var userData       = serializer.Serialize(serializeModel);
            var authTicket     = new FormsAuthenticationTicket(1, user.Code, DateTime.Now, DateTime.Now.AddHours(2), false, userData);
            var eticket        = FormsAuthentication.Encrypt(authTicket);
            var fcookie        = new HttpCookie(FormsAuthentication.FormsCookieName, eticket);

            Response.Cookies.Add(fcookie);

            // Update Last Login Time
            if (user.AccessRule != null)
            {
                user.AccessRule.LastLoginDate    = DateTime.UtcNow;
                user.AccessRule.LastActivityDate = DateTime.UtcNow;
                user.AccessRule.PasswordFailuresSinceLastSuccess = 0;
                user.AccessRule.LastLoginDate = user.AccessRule.LastActivityDate = DateTime.UtcNow;

                _userRepository.Update(user);
                _unitOfWork.Commit();
            }
        }
Esempio n. 3
0
        public JsonResult Index(RoleFilterModel vm)
        {
            if (vm.PermissionId.HasValue)
            {
                var apiResult = TryExecute(() =>
                {
                    return(_rolePermissionRepository.GetAllBy(p => p.PermissionId == vm.PermissionId.Value, o => o.OrderByDescending(l => l.CreatedOn), "Role").Select(p => p.Role).ToList());
                }, "Roles Fetched sucessfully");

                return(Json(apiResult, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var apiResult = TryExecute(() =>
                {
                    return(_roleRepository.GetAll(o => o.OrderByDescending(l => l.CreatedOn)));
                }, "Roles Fetched sucessfully");

                return(Json(apiResult, JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 4
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var authCookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                var serializer = new JavaScriptSerializer();
                if (authTicket != null)
                {
                    var sm       = serializer.Deserialize <PrincipalModel>(authTicket.UserData);
                    var userInfo = HttpSessionWrapper.GetUserInfo(sm.Key);

                    if (userInfo == null)
                    {
                        // Couldn't get it from Session, get it from database & cache it
                        var selectedUser = _userRepository.GetBy(u => u.Code == sm.Key, "Person");
                        if (selectedUser != null)
                        {
                            var userRoles = _roleMemberRepository.GetAllBy(m => m.UserId == selectedUser.Id)
                                            .Select(r => r.RoleId)
                                            .ToList();
                            var permissions = _rolePermissionRepository.GetAllBy(r => userRoles.Contains(r.RoleId), "Permission")
                                              .Select(p => p.Permission.PermissionCode)
                                              .ToList();

                            userInfo = UserInfo.GetInstance(selectedUser, permissions);
                            HttpSessionWrapper.SetInSession(selectedUser.Code, userInfo);

                            var newUser = new Principal(userInfo.Name, sm.Key)
                            {
                                Name        = userInfo.Name,
                                Id          = userInfo.Id,
                                Permissions = permissions
                            };

                            filterContext.HttpContext.User = newUser;
                        }
                    }
                    else
                    {
                        var newUser = new Principal(userInfo.Name, sm.Key)
                        {
                            Name        = userInfo.Name,
                            Id          = userInfo.Id,
                            Permissions = userInfo.Permissions
                        };

                        HttpContext.Current.User = newUser;
                    }
                }
            }
        }
Esempio n. 5
0
        public ActionResult Details(int id)
        {
            var permission = _permissionRepository.Get(id);
            var vm         = new PermissionDetailsViewModel(permission);

            var roles   = _rolePermissionRepository.GetAllBy(p => p.PermissionId == permission.Id, "Role").Select(p => p.Role).ToList();
            var roleIds = roles.Select(r => r.Id).ToList();

            var userIds = _roleMemberRepository.GetAllBy(m => roleIds.Contains(m.RoleId)).Select(u => u.UserId).ToList();
            var users   = _userRepository.GetAllBy(u => userIds.Contains(u.Id), "Person").ToList();

            vm.Roles = roles;
            vm.Users = users;

            return(View(vm));
        }
Esempio n. 6
0
        public Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            var headers    = actionContext.Request.Headers;
            var authCookie = headers
                             .GetCookies()
                             .Select(c => c[FormsAuthentication.FormsCookieName])
                             .FirstOrDefault();

            if (authCookie != null)
            {
                var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                var serializer = new JavaScriptSerializer();
                if (authTicket != null)
                {
                    var sm = serializer.Deserialize <PrincipalModel>(authTicket.UserData);

                    // Couldn't get it from Session, get it from database & cache it
                    var selectedUser = _userRepository.GetBy(u => u.Code == sm.Key, "Person");
                    if (selectedUser != null)
                    {
                        var userRoles = _roleMemberRepository.GetAllBy(m => m.UserId == selectedUser.Id)
                                        .Select(r => r.RoleId)
                                        .ToList();
                        var permissions = _rolePermissionRepository.GetAllBy(r => userRoles.Contains(r.RoleId), "Permission")
                                          .Select(p => p.Permission.PermissionCode)
                                          .ToList();

                        var userInfo = UserInfo.GetInstance(selectedUser, permissions);

                        var newUser = new Principal(userInfo.Name, sm.Key)
                        {
                            Name        = userInfo.Name,
                            Id          = userInfo.Id,
                            Permissions = permissions
                        };

                        Thread.CurrentPrincipal  = newUser;
                        HttpContext.Current.User = newUser;
                    }
                }
            }

            return(Task.FromResult(0));
        }