예제 #1
0
        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                var    routeData       = ((MvcHandler)httpContext.Handler).RequestContext.RouteData;
                object currentAreaName = string.Empty;
                routeData.Values.TryGetValue("area", out currentAreaName);
                object currentControllerName = string.Empty;
                routeData.Values.TryGetValue("controller", out currentControllerName);
                object currentActionName = string.Empty;
                routeData.Values.TryGetValue("action", out currentActionName);
                //if (currentControllerName.ToString(true).ToLower() == Constants.CommonConstants.DefaultControllerName.ToLower() && currentActionName.ToString(true).ToLower() == Constants.CommonConstants.DefaultActionName.ToLower())
                //{
                //    return true;
                //}
                Worker loggedInWorker = WebHelper.CurrentSession.Content.LoggedInWorker;
                if (loggedInWorker == null)
                {
                    IWorkerRepository workerRepository = DependencyResolver.Current.GetService(typeof(IWorkerRepository)) as IWorkerRepository;

                    loggedInWorker = workerRepository.Find(httpContext.User.Identity.Name);
                    WebHelper.CurrentSession.Content.LoggedInWorker = loggedInWorker;
                }
                if (loggedInWorker != null)
                {
                    return(true);
                }
            }
            return(base.AuthorizeCore(httpContext));
        }
        public Worker AuthenticateWorker(string userName, string password, bool rememberMe, bool isExternalLogin = false)
        {
            try
            {
                if (userName.IsNullOrEmpty())
                {
                    throw new CustomException(CustomExceptionType.CommonArgumentNullException, "Enter user name");
                }
                if (password.IsNullOrEmpty())
                {
                    throw new CustomException(CustomExceptionType.CommonArgumentNullException, "Enter password");
                }
                string errorMessage = string.Empty;
                if (workerRepository != null)
                {
                    string originalPassword = password;
                    password = CryptographyHelper.Encrypt(password);
                    Worker loggedInWorker = workerRepository.Find(userName, password);
                    if (loggedInWorker != null)
                    {
                        if (loggedInWorker.IsActive)
                        {
                            if (loggedInWorker.AllowLogin)
                            {
                                loggedInWorker.ConfirmPassword = loggedInWorker.Password;
                                loggedInWorker.LastLoginDate   = DateTime.Now;
                                workerRepository.InsertOrUpdate(loggedInWorker);
                                workerRepository.Save();
                                if (!isExternalLogin)
                                {
                                    WebHelper.CurrentSession.Content.LoggedInWorker = loggedInWorker;
                                    List <int> roleIDs   = null;
                                    List <int> regionIDs = null;

                                    roleIDs   = workerinrolenewRepository.FindAllActiveWorkerInRoleByWorkerID();
                                    regionIDs = workerinrolenewRepository.FindAllActiveRegionByWorkerID();
                                    //List<WorkerInRole> workerRoles = workerinroleRepository.FindAllActiveByWorkerID(loggedInWorker.ID);

                                    //if (workerRoles != null)
                                    //{
                                    //    foreach (WorkerInRole workerRole in workerRoles)
                                    //    {
                                    //        if (!roleIDs.Contains(workerRole.WorkerRoleID.ToString()))
                                    //        {
                                    //            roleIDs = roleIDs.Concate(',', workerRole.WorkerRoleID.ToString());
                                    //        }
                                    //        if (!regionIDs.Contains(workerRole.RegionID.ToString()))
                                    //        {
                                    //            regionIDs = regionIDs.Concate(',', workerRole.RegionID.ToString());
                                    //        }
                                    //    }
                                    //}

                                    if (roleIDs == null)
                                    {
                                        throw new CustomException(CustomExceptionType.CommonArgumentNullException, "There is no role assigned to the user");
                                    }
                                    WebHelper.CurrentSession.Content.LoggedInWorkerRoleIDs   = roleIDs;
                                    WebHelper.CurrentSession.Content.LoggedInWorkerRegionIDs = regionIDs;
                                    VisibilityStatus regionVisiblity     = VisibilityStatus.UnDefined;
                                    VisibilityStatus programVisiblity    = VisibilityStatus.UnDefined;
                                    VisibilityStatus subProgramVisiblity = VisibilityStatus.UnDefined;
                                    VisibilityStatus caseVisiblity       = VisibilityStatus.UnDefined;
                                    //workerRolePermissionRepository.FindVisiblity(loggedInWorker.ID, ref regionVisiblity, ref programVisiblity, ref subProgramVisiblity, ref caseVisiblity);
                                    workerRolePermissionNewRepository.FindVisiblity(loggedInWorker.ID, ref regionVisiblity, ref programVisiblity, ref subProgramVisiblity, ref caseVisiblity);
                                    WebHelper.CurrentSession.Content.RegionVisibility     = regionVisiblity;
                                    WebHelper.CurrentSession.Content.ProgramVisibility    = programVisiblity;
                                    WebHelper.CurrentSession.Content.SubProgramVisibility = subProgramVisiblity;
                                    WebHelper.CurrentSession.Content.CaseVisibility       = caseVisiblity;

                                    CookieHelper newCookieHelper = new CookieHelper();
                                    newCookieHelper.SetLoginCookie(userName, loggedInWorker.ID.ToString(), rememberMe);
                                    if (rememberMe)
                                    {
                                        newCookieHelper.RememberMe(userName, originalPassword);
                                    }
                                    else
                                    {
                                        newCookieHelper.ForgetMe();
                                    }
                                }
                                //loggedInWorker = setUserPermission(loggedInWorker);
                                return(loggedInWorker);
                            }
                            else
                            {
                                throw new CustomException(CustomExceptionType.CommonArgumentNullException, "User access has been blocked by administrator");
                            }
                        }
                        else
                        {
                            throw new CustomException(CustomExceptionType.CommonArgumentNullException, "User has not been activated yet");
                        }
                    }
                    else
                    {
                        throw new CustomException(CustomExceptionType.CommonArgumentNullException, "Invalid user name/password");
                    }
                }
                return(null);
            }
            catch (CustomException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new CustomException(CustomExceptionType.UserLoginUnknownError, Constants.Messages.UserLogin_UnknownError, ex);
            }
        }