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);
            }
        }
Exemplo n.º 2
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            object objCurrentControllerName = string.Empty;

            this.RouteData.Values.TryGetValue("controller", out objCurrentControllerName);
            object objCurrentActionName = string.Empty;

            this.RouteData.Values.TryGetValue("action", out objCurrentActionName);
            object currentAreaName = string.Empty;

            this.RouteData.Values.TryGetValue("Areas", out currentAreaName);
            if (this.RouteData.DataTokens.ContainsKey("area"))
            {
                currentAreaName = this.RouteData.DataTokens["area"].ToString();
            }
            string currentActionName     = objCurrentActionName.ToString(true);
            string currentControllerName = objCurrentControllerName.ToString(true);

            ViewBag.HasAccessToOtherConfigurationData = false;
            ViewBag.CurrentActionName     = currentActionName;
            ViewBag.CurrentControllerName = currentControllerName;
            ViewBag.CurrentAreaName       = currentAreaName;

            ViewBag.HasAccessToWorkerModule         = false;
            ViewBag.HasAccessToReportModule         = false;
            ViewBag.HasAccessToAdminModule          = false;
            ViewBag.HasAccessToCaseManagementModule = true;
            ViewBag.IsRegionalAdministrator         = false;

            if (CurrentLoggedInWorker != null)
            {
                ViewBag.CurrentWorkerID = CurrentLoggedInWorker.ID;
                //ViewBag.CurrentWorkerRoleID = CurrentLoggedInWorker.UserRoleID;
                ViewBag.CurrentWorkerName = CurrentLoggedInWorker.FirstName + " " + CurrentLoggedInWorker.LastName;
            }
            currentActionName = currentActionName.ToLower();
            if (!currentActionName.Contains("ajax"))
            {
                //<JL:Comment:06/13/2017>
                //if (WebHelper.CurrentSession.Content.RegionVisibility == VisibilityStatus.UnDefined && workerRolePermissionRepository != null)
                if (WebHelper.CurrentSession.Content.RegionVisibility == VisibilityStatus.UnDefined && workerRolePermissionNewRepository != null)
                {
                    VisibilityStatus regionVisiblity     = VisibilityStatus.UnDefined;
                    VisibilityStatus programVisiblity    = VisibilityStatus.UnDefined;
                    VisibilityStatus subProgramVisiblity = VisibilityStatus.UnDefined;
                    VisibilityStatus caseVisiblity       = VisibilityStatus.UnDefined;
                    workerRolePermissionNewRepository.FindVisiblity(CurrentLoggedInWorker.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;
                }
            }

            ViewBag.CurrentLoggedInWorkerRoleIDs = CurrentLoggedInWorkerRoleIDs;

            if (CurrentLoggedInWorkerRoleIDs != null && CurrentLoggedInWorkerRoleIDs.IndexOf(1) != -1)
            {
                ViewBag.HasAccessToWorkerModule = true;
                ViewBag.HasAccessToReportModule = true;
                ViewBag.HasAccessToAdminModule  = true;
            }

            //<JL:Add:06/11/2017>
            if (workerroleactionpermissionnewRepository != null)
            {
                bool HasWorkerMenuPermission = workerroleactionpermissionnewRepository.HasPermission(CurrentLoggedInWorkerRoleIDs, Constants.Areas.WorkerManagement, Constants.Controllers.Worker, string.Empty, true);
                if (HasWorkerMenuPermission)
                {
                    ViewBag.HasAccessToWorkerModule = true;
                }
                bool HasReportMenuPermission = workerroleactionpermissionnewRepository.HasPermission(CurrentLoggedInWorkerRoleIDs, Constants.Areas.Reporting, Constants.Controllers.Report, string.Empty, true);
                if (HasReportMenuPermission)
                {
                    ViewBag.HasAccessToReportModule = true;
                }
            }
            //</JL:Add:06/11/2017>

            if (CurrentLoggedInWorkerRoleIDs != null && CurrentLoggedInWorkerRoleIDs.IndexOf(SiteConfigurationReader.RegionalManagerRoleID) != -1)
            {
                ViewBag.HasAccessToWorkerModule           = true;
                ViewBag.HasAccessToReportModule           = true;
                ViewBag.HasAccessToAdminModule            = false;
                ViewBag.HasAccessToCaseManagementModule   = false;
                ViewBag.IsRegionalAdministrator           = true;
                ViewBag.HasAccessToOtherConfigurationData = true;
            }

            base.OnActionExecuting(filterContext);
        }