Exemple #1
0
        public ActionResult UserLogin(Models.Security.LoginModel vm)
        {
            bool isMailvalid = false;

            if (vm != null)
            {
                if (vm.loginID == null || vm.loginID.Length < 1 || vm.loginPassword == null || vm.loginPassword.Length < 1)
                {
                    ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                    TempData["loginError"] = loginError;
                    return(RedirectToAction("index", "Home"));
                }
                //since the user can connect using the email address and the id, we need to validate first if the email address is correct
                //if it is, then we search based on the email addreess, if not, we try to search based on the id, if the id is not valid also, then we failed and return tu user
                //validate the Email Address by using the MailAddress class from .Net Framework.
                //if the email Address received from the view is accurate, then it will create a instance of the MailAddress class. If not will fail
                //MailAddress mailAddress = new MailAddress(vm.loginID);
                //isMailvalid = (mailAddress.Address == vm.loginID);
                if (vm.loginID.Contains("@"))
                {
                    isMailvalid = true;
                }
                ;

                if (isMailvalid)
                {
                    if (ModelState.IsValid)
                    {
                        //try to get the details from the database
                        DBContext.User user = dbmodel.Users.Where(x => x.EmailAddress == vm.loginID && x.Password == vm.loginPassword).FirstOrDefault();
                        if (user != null)
                        {
                            //Get the settings for the useer
                            // Models.Security.UserSettings userSettings = new Models.Security.UserSettings(dbmodel.User_Settings.Where(x => x.ID == user.ID).FirstOrDefault());
                            Models.Security.User      userModel = Models.Security.User.CreateUser(user);
                            Models.Security.AuthState userState = new Models.Security.AuthState(userModel, vm.timezone);
                            userState.LogIn();
                            SetUserSession(userModel, userState);

                            return(RedirectToAction("Overview", "Portal"));
                        }
                        else
                        {
                            //Create Error Model
                            ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                            TempData["loginError"] = loginError;
                            return(RedirectToAction("index", "Home"));
                        }
                    }
                    //if the model is not valid, return back to user
                    else
                    {
                        ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                        TempData["loginError"] = loginError;
                        return(RedirectToAction("index", "Home"));
                    }
                }
                else //get user by the ID since the email failed.
                {
                    DBContext.User user = dbmodel.Users.Where(x => x.ID == vm.loginID && x.Password == vm.loginPassword).FirstOrDefault();
                    if (user != null)
                    {
                        //Get the settings for the useer
                        //Models.Security.UserSettings userSettings = new Models.Security.UserSettings(dbmodel.User_Settings.Where(x => x.ID == user.ID).FirstOrDefault());
                        Models.Security.User      userModel = Models.Security.User.CreateUser(user);
                        Models.Security.AuthState userState = new Models.Security.AuthState(userModel, vm.timezone);
                        userState.LogIn();

                        SetUserSession(userModel, userState);

                        return(RedirectToAction("Overview", "Portal"));
                    }
                    else
                    {
                        ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("IDIssue", true, "The Login Credential are not correct!");
                        TempData["loginError"] = loginError;
                        return(RedirectToAction("index", "Home"));
                    }
                }
            }
            else
            {
                ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("Datasend", true, "The Login Credential are not correct!");
                TempData["loginError"] = loginError;
                return(RedirectToAction("Error"));
            }
        }
Exemple #2
0
 private void SetUserSession(Models.Security.User user, Models.Security.AuthState userState)
 {
     Session["User"]        = user;
     Session["DisplayName"] = user.FirstName;
     Session["AuthState"]   = userState;
 }
        // GET: Portal
        public ActionResult Overview()
        {
            //Get the user from the session
            Models.Security.User      user      = (Models.Security.User)Session["User"];
            Models.Security.AuthState userState = (Models.Security.AuthState)Session["AuthState"];

            if (user == null || userState == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            const int defaultDays    = -7;
            DateTime  todayDate      = DateTime.UtcNow;
            DateTime  sevenDaysBackT = todayDate.AddDays(defaultDays);
            DateTime  svn            = new DateTime(sevenDaysBackT.Year, sevenDaysBackT.Month, sevenDaysBackT.Day, 0, 0, 0);
            DateTime  yesterdayDate  = new DateTime(todayDate.AddDays(-1).Year, todayDate.AddDays(-1).Month, todayDate.AddDays(-1).Day, 23, 59, 59);

            //Get the list of records for the past default Days.
            List <DBContext.Main_Data>      records     = db.Main_Data.Where(x => x.Status_Start_Time >= svn && x.Status_End_Time <= yesterdayDate && x.User_ID == user.ID).ToList();
            List <Models.Portal.WorkRecord> workRecords = new List <Models.Portal.WorkRecord>();

            foreach (var record in records)
            {
                var newWorkRecord = new Models.Portal.WorkRecord(record);
                workRecords.Add(newWorkRecord);
            }

            var dailyTotalHours = Models.Portal.TimeSheet.CalculateTotalHoursForPeriod(workRecords, defaultDays, todayDate);

            //Get the stats for today;

            var todayRecords = db.Main_Data.Where(x => x.CurrentDate.Day == todayDate.Day && x.CurrentDate.Month == todayDate.Month && x.CurrentDate.Year == todayDate.Year && x.User_ID == user.ID).ToList();
            List <Models.Portal.WorkRecord> todayRecordsModel = new List <Models.Portal.WorkRecord>();

            foreach (var record in todayRecords)
            {
                var newWorkRecord = new Models.Portal.WorkRecord(record);
                todayRecordsModel.Add(newWorkRecord);
            }

            var userSettings = new Models.Security.UserSettings();


            //get top five public holidays
            var publicHolidaysDb = db.Public_Holidays.Where(x => x.Holiday_country == user.Country && x.Holiday_date >= todayDate.Date).Take(5).ToList();
            List <Models.Portal.PublicHoliday> publicHolidays = new List <Models.Portal.PublicHoliday>();

            DateTime[] publicHolidaysAsArray = new DateTime[publicHolidaysDb.Count];
            int        counter = 0;

            foreach (var holiday in publicHolidaysDb)
            {
                publicHolidaysAsArray[counter] = holiday.Holiday_date.Value.Date;
                publicHolidays.Add(new Models.Portal.PublicHoliday(holiday));
                counter++;
            }


            //calculate work time for the current month;
            int currentMonth = todayDate.Month;

            var monthlyRecords = db.Main_Data.Where(x => x.CurrentDate.Day >= 1 && x.CurrentDate.Month == currentMonth && x.CurrentDate.Year == todayDate.Year && x.CurrentDate.Day <= 31 && x.User_ID == user.ID).ToList();
            var recordsModel   = new List <Models.Portal.WorkRecord>();

            foreach (var record in monthlyRecords)
            {
                var newWorkRecord = new Models.Portal.WorkRecord(record);
                recordsModel.Add(newWorkRecord);
            }


            //Get the leaves status
            List <DBContext.Leave>            thisYearleaves = db.Leaves.Where(x => x.StartDate.Value >= todayDate.Date && x.UserId == user.ID).ToList();
            List <Models.Portal.Leave.ILeave> leaves         = new List <Models.Portal.Leave.ILeave>();

            foreach (var leave in thisYearleaves)
            {
                var newleave = new Models.Portal.Leave.Leave(leave, publicHolidaysAsArray);
                leaves.Add(newleave);
            }


            //get user leaves status(entitled, accrued, carried over)
            List <DBContext.User_Leaves_Status>  userLeavesStatusDb = db.User_Leaves_Status.Where(x => x.UserId == user.ID).ToList();
            List <Models.Portal.UserLeaveStatus> userLeaveStatus    = new List <Models.Portal.UserLeaveStatus>();

            foreach (var item in userLeavesStatusDb)
            {
                var newLeave = new Models.Portal.UserLeaveStatus(item);
                userLeaveStatus.Add(newLeave);
            }

            //Set User Stats and calculate the data
            var userStats = new Models.Portal.UserStatus(userSettings, todayRecordsModel, leaves, userLeaveStatus);

            userStats.totalWorkedHoursPerMonth = userStats.CalculateWorkTimeForGivenList(recordsModel);
            userStats.CalculateStats();



            //Get user Alerts!
            List <DBContext.Alert> alerts = db.Alerts.Where(x => x.UserID == user.ID || (x.ManagerId == user.ManagerID && x.IsForEveryone == true)).ToList();

            //Filter the list;
            alerts = alerts.Where(x => (x.StartDate == null || x.EndDate == null) || x.EndDate.Value.Date > todayDate.Date).ToList();


            List <Models.Portal.Alert> alertList = new List <Models.Portal.Alert>();

            foreach (var alert in alerts)
            {
                var newAlert = new Models.Portal.Alert(alert);
                alertList.Add(newAlert);
            }

            //Generate AutoAlerts and add them to the list of the already existing one
            var autoAlerts = Models.Portal.AutoGeneratedAlerts.GenerateAlerts(user);

            alertList.AddRange(autoAlerts);



            //Get the requested leaves
            List <DBContext.Leave_Requests> requestedLeavesDb = db.Leave_Requests.Where(x => x.Requestor == user.ID && x.RequestStartDate.Value >= todayDate.Date).ToList();

            foreach (var leave in requestedLeavesDb)
            {
                var newLeave = new Models.Portal.Leave.LeaveRequest(leave, publicHolidaysAsArray);
                leaves.Add(newLeave);
            }

            //Get the requested leaves
            List <DBContext.Leaves_Saved_Not_Submited> savedLeavesDb = db.Leaves_Saved_Not_Submited.Where(x => x.UserID == user.ID && x.StartDate.Value >= todayDate.Date).ToList();

            foreach (var leave in savedLeavesDb)
            {
                var newLeave = new Models.Portal.Leave.SavedLeave(leave, publicHolidaysAsArray);
                leaves.Add(newLeave);
            }



            leaves = leaves.OrderBy(x => x.start).Take(5).ToList();
            var dashboard = new Models.Portal.Dashboard(user, dailyTotalHours, userStats, leaves, alertList, publicHolidays);


            return(View(dashboard));
        }
        public JsonResult ChangePeriodForChart(int periodType)
        {
            if (periodType == 0)
            {
                return(Json(new { reuslt = new Dictionary <int, double>() }, JsonRequestBehavior.AllowGet));
            }

            Models.Security.User      user      = (Models.Security.User)Session["User"];
            Models.Security.AuthState userState = (Models.Security.AuthState)Session["AuthState"];

            if (user == null || userState == null)
            {
                //passs the redirect to the view, in order to send the user back to the login page.
                //further will have to build a way whwere we log out the user if it doesnot do anything on the page.
                //build a solution to keep the user logged in if he actually using the website. like always reseting the timmer for the session variable
                return(Json(new { result = false, redirectUrl = Url.Action("Index", "Home"), isRedirect = true }, JsonRequestBehavior.AllowGet));
            }


            int defaultDays = 0;

            switch (periodType)
            {
            case 1:
                defaultDays = -7;
                break;

            case 2:
                defaultDays = -14;
                break;

            case 3:
                defaultDays = -30;
                break;

            default:
                defaultDays = 0;
                break;
            }

            DateTime todayDate      = DateTime.UtcNow;
            DateTime sevenDaysBackT = todayDate.AddDays(defaultDays);
            DateTime svn            = new DateTime(sevenDaysBackT.Year, sevenDaysBackT.Month, sevenDaysBackT.Day, 0, 0, 0);
            DateTime yesterdayDate  = new DateTime(todayDate.AddDays(-1).Year, todayDate.AddDays(-1).Month, todayDate.AddDays(-1).Day, 23, 59, 59);

            try
            {
                //Get the list of records for the past default Days.
                List <DBContext.Main_Data>      records     = db.Main_Data.Where(x => x.Status_Start_Time >= svn && x.Status_End_Time <= yesterdayDate && x.User_ID == user.ID).ToList();
                List <Models.Portal.WorkRecord> workRecords = new List <Models.Portal.WorkRecord>();
                foreach (var record in records)
                {
                    var newWorkRecord = new Models.Portal.WorkRecord(record);
                    workRecords.Add(newWorkRecord);
                }

                var dailyTotalHours = Models.Portal.TimeSheet.CalculateTotalHoursForPeriod(workRecords, defaultDays, todayDate);

                return(Json(new { result = true, data = dailyTotalHours.ToList() }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new { result = false, message = ex.Message }, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #5
0
        public ActionResult MyTimeSheet()
        {
            Models.Security.User      user      = (Models.Security.User)Session["User"];
            Models.Security.AuthState userState = (Models.Security.AuthState)Session["AuthState"];

            if (user == null || userState == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var worklogsDb = db.Main_Data.Where(x => x.User_ID == user.ID).OrderByDescending(x => x.Status_Start_Time).Take(100);

            List <Models.Portal.WorkRecord> workLogs = new List <Models.Portal.WorkRecord>();

            foreach (var worklog in worklogsDb)
            {
                var newWorkLog = new Models.Portal.WorkRecord(worklog);
                workLogs.Add(newWorkLog);
            }
            //Get today date in UTC Format
            DateTime todayDate = DateTime.UtcNow.Date;

            //Get the public holidays
            var publicHolidaysDb = db.Public_Holidays.Where(x => x.Holiday_country == user.Country && x.Holiday_date >= todayDate.Date).ToList();
            List <Models.Portal.PublicHoliday> publicHolidays = new List <Models.Portal.PublicHoliday>();

            DateTime[] publicHolidaysAsArray = new DateTime[publicHolidaysDb.Count];
            int        counter = 0;

            foreach (var holiday in publicHolidaysDb)
            {
                publicHolidaysAsArray[counter] = holiday.Holiday_date.Value.Date;
                publicHolidays.Add(new Models.Portal.PublicHoliday(holiday));
                counter++;
            }


            List <DBContext.Leave>            thisYearleaves = db.Leaves.Where(x => x.StartDate.Value.Year == todayDate.Year && x.UserId == user.ID && x.EndDate.Value.Year == todayDate.Year).ToList();
            List <Models.Portal.Leave.ILeave> leaves         = new List <Models.Portal.Leave.ILeave>();

            foreach (var leave in thisYearleaves)
            {
                var newleave = new Models.Portal.Leave.Leave(leave, publicHolidaysAsArray);
                leaves.Add(newleave);
            }


            //Get the requested leaves
            List <DBContext.Leave_Requests> requestedLeavesDb = db.Leave_Requests.Where(x => x.Requestor == user.ID && x.RequestStartDate.Value >= todayDate.Date).ToList();

            foreach (var leave in requestedLeavesDb)
            {
                var newLeave = new Models.Portal.Leave.LeaveRequest(leave, publicHolidaysAsArray);
                leaves.Add(newLeave);
            }

            //Get the requested leaves
            List <DBContext.Leaves_Saved_Not_Submited> savedLeavesDb = db.Leaves_Saved_Not_Submited.Where(x => x.UserID == user.ID && x.StartDate.Value >= todayDate.Date).ToList();

            foreach (var leave in savedLeavesDb)
            {
                var newLeave = new Models.Portal.Leave.SavedLeave(leave, publicHolidaysAsArray);
                leaves.Add(newLeave);
            }



            //get a list of projects that the user is assign to
            var projectsDb = db.User_Assigned_Projects.Where(x => x.UserID == user.ID).ToList();
            List <Models.Portal.Project> projectsAssignedToUser = new List <Models.Portal.Project>();

            //add one item as default All projects with the value 0
            projectsAssignedToUser.Add(new Models.Portal.Project(0, "All projects", user.ID));

            foreach (var project in projectsDb)
            {
                projectsAssignedToUser.Add(
                    new Models.Portal.Project(project)
                    );
            }



            //Implement Dependency injection
            //Mostlikly is not needed
            var cb = new ContainerBuilder();

            //cb.RegisterType<List<Models.Portal.WorkRecord>>();
            //cb.RegisterType<List<Models.Portal.Leave.ILeave>>();

            cb.RegisterInstance(workLogs).As <List <Models.Portal.WorkRecord> >();
            cb.RegisterInstance(leaves).As <List <Models.Portal.Leave.ILeave> >();
            cb.RegisterInstance(projectsAssignedToUser).As <List <Models.Portal.Project> >();

            cb.RegisterType <Models.Activity.MyActivity>();

            var builder    = cb.Build();
            var myActivity = builder.Resolve <Models.Activity.MyActivity>();


            //Models.Activity.MyActivity myActivity = new Models.Activity.MyActivity();
            //myActivity.workLogs = workLogs;
            //myActivity.leaves = leaves;

            //Add values to session for further use when the user change the number of records to show
            SetMyStatSession(myActivity);
            Session.Timeout = 120;

            return(View(myActivity));
        }