Exemplo n.º 1
0
        public ActionResult AcademicYearUpdate(int id)
        {
            int userid = Convert.ToInt32(Session["UserID"].ToString());
            CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == id).FirstOrDefault();

            Session["AcademicYear"]   = companyacademicyear.CurrentAcademicYear.Name;
            Session["AcademicYearID"] = companyacademicyear.CurrentAcademicYear.AcademicYearID;
            //Session["CompanyAcademicYearID"] = companyacademicyear.CompanyAcademicYearID;
            Session["AcademicYearList"] = db.AcademicYears.ToList();
            return(Redirect(Request.QueryString["ReturnUrl"]));
        }
        // GET api/TimeTable
        public IHttpActionResult GetTimeTables(int userid, int academicyearid, int schoolid)
        {
            //var teacherlist = db.Teachers.ToJsonArray();
            //var response = Request.CreateResponse(HttpStatusCode.OK);
            //response.Content = new StringContent(Json(teacherlist), Encoding.UTF8, "application/json");

            CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == academicyearid).FirstOrDefault();
            int    companyacademicyearid            = companyacademicyear.CompanyAcademicYearID;
            var    userroles  = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == userid).FirstOrDefault();
            int    roleid     = userroles.CurrentRole.RoleID;
            School school     = db.Schools.Find(schoolid);
            var    timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.TimeTableID == 0).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();

            switch (roleid)
            {
            case 1:
                timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.CurrentSchool.CompanyAcademicYearID == companyacademicyearid).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();
                break;

            case 2:

                timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.CurrentSchool.CompanyAcademicYearID == companyacademicyearid && t.CurrentSchool.SchoolID == schoolid).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();
                break;

            case 3:
                Teacher teacher = db.Teachers.Where(t => t.UserID == userid).FirstOrDefault();
                timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.CurrentSchool.CompanyAcademicYearID == companyacademicyearid && t.CurrentTeacher.TeacherID == teacher.TeacherID).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();
                break;

            case 4:

                timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.CurrentSchool.CompanyAcademicYearID == companyacademicyearid && t.CurrentSchool.SchoolID == schoolid).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();
                break;

            case 5:
                Student student = db.Students.Where(t => t.UserID == userid).FirstOrDefault();
                timeTables = db.TimeTables.Include(t => t.CurrentSchool).Include(t => t.CurrentClass).Include(t => t.CurrentPeriod).Include(t => t.CurrentTeacher).Include(t => t.CurrentSubject).Where(t => t.CurrentSchool.CompanyAcademicYearID == companyacademicyearid && t.CurrentClass.ClassID == student.ClassID).OrderBy(t => t.CurrentPeriod.PeriodID).ToList();
                break;

            default:
                break;
            }
            return(Json(timeTables));
        }
        // GET api/Parent
        public IHttpActionResult GetParents(int userid, int academicyearid, int schoolid)
        {
            //var parentlist = db.Parents.ToJsonArray();
            //var response = Request.CreateResponse(HttpStatusCode.OK);
            //response.Content = new StringContent(Json(parentlist), Encoding.UTF8, "application/json");
            CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == academicyearid).FirstOrDefault();
            int companyacademicyearid = companyacademicyear.CompanyAcademicYearID;
            var userroles             = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == userid).FirstOrDefault();
            int roleid  = userroles.CurrentRole.RoleID;
            var parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid).Where(s => s.ParentID == 0).ToList();

            switch (roleid)
            {
            case 1:
                parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid).OrderBy(s => s.Name).ToList();
                break;

            case 2:
                parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == schoolid).OrderBy(s => s.Name).ToList();
                break;

            case 3:
                parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == schoolid).OrderBy(s => s.Name).ToList();
                break;

            case 4:
                parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.UserID == userid).OrderBy(s => s.Name).ToList();
                break;

            case 5:
                Student student  = db.Students.Where(s => s.UserID == userid).FirstOrDefault();
                int     parentid = student.ParentID;
                parents = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentSchool.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.ParentID == parentid).OrderBy(s => s.Name).ToList();
                break;

            default:
                break;
            }
            return(Json(parents));
        }
        public IHttpActionResult GetProfileApi()
        {
            try
            {
                apiheaderdata       apidata             = APIAuthorizeAttribute.GetAuthorize(Request.Headers.GetValues("Token").First());
                CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == apidata.AcademicYearID).FirstOrDefault();
                int      companyacademicyearid          = companyacademicyear.CompanyAcademicYearID;
                UserRole userrole = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == apidata.UserID).FirstOrDefault();
                int      roleid   = userrole.RoleID;

                switch (roleid)
                {
                case 1:
                    School         school    = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.SchoolID == apidata.SchoolID).FirstOrDefault();
                    SchoolModelApi schoolapi = new SchoolModelApi();
                    schoolapi.SchoolID     = school.SchoolID;
                    schoolapi.Name         = school.Name;
                    schoolapi.ShortName    = school.ShortName;
                    schoolapi.HouseNo      = school.HouseNo;
                    schoolapi.BuildingName = school.BuildingName;
                    schoolapi.Street       = school.Street;
                    schoolapi.Area         = school.Area;
                    schoolapi.Location     = school.Location;
                    schoolapi.City         = school.CurrentCity.Name;
                    schoolapi.Country      = school.CurrentCountry.Name;
                    schoolapi.PinCode      = school.PinCode;
                    schoolapi.Landline     = school.Landline;
                    schoolapi.Mobile       = school.Mobile;
                    schoolapi.RegNo        = school.RegNo;
                    schoolapi.Logo         = school.Logo;
                    schoolapi.Principal    = school.Principal;
                    schoolapi.Status       = school.Status;
                    schoolapi.Email        = school.CurrentUser.Email;
                    schoolapi.Password     = school.CurrentUser.Password;
                    return(Json(schoolapi));

                case 2:
                    school                 = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.SchoolID == apidata.SchoolID).FirstOrDefault();
                    schoolapi              = new SchoolModelApi();
                    schoolapi.SchoolID     = school.SchoolID;
                    schoolapi.Name         = school.Name;
                    schoolapi.ShortName    = school.ShortName;
                    schoolapi.HouseNo      = school.HouseNo;
                    schoolapi.BuildingName = school.BuildingName;
                    schoolapi.Street       = school.Street;
                    schoolapi.Area         = school.Area;
                    schoolapi.Location     = school.Location;
                    schoolapi.City         = school.CurrentCity.Name;
                    schoolapi.Country      = school.CurrentCountry.Name;
                    schoolapi.PinCode      = school.PinCode;
                    schoolapi.Landline     = school.Landline;
                    schoolapi.Mobile       = school.Mobile;
                    schoolapi.RegNo        = school.RegNo;
                    schoolapi.Logo         = school.Logo;
                    schoolapi.Principal    = school.Principal;
                    schoolapi.Status       = school.Status;
                    schoolapi.Email        = school.CurrentUser.Email;
                    schoolapi.Password     = school.CurrentUser.Password;
                    return(Json(schoolapi));

                case 3:
                    Teacher         teacher    = db.Teachers.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                    TeacherModelApi teacherapi = new TeacherModelApi();
                    teacherapi.TeacherID     = teacher.TeacherID;
                    teacherapi.Name          = teacher.Name;
                    teacherapi.HouseNo       = teacher.HouseNo;
                    teacherapi.BuildingName  = teacher.BuildingName;
                    teacherapi.Street        = teacher.Street;
                    teacherapi.Area          = teacher.Area;
                    teacherapi.Location      = teacher.Location;
                    teacherapi.City          = teacher.CurrentCity.Name;
                    teacherapi.Country       = teacher.CurrentCountry.Name;
                    teacherapi.PinCode       = teacher.PinCode;
                    teacherapi.Landline      = teacher.Landline;
                    teacherapi.Mobile        = teacher.Mobile;
                    teacherapi.Photo         = teacher.Photo;
                    teacherapi.School        = teacher.CurrentSchool.Name;
                    teacherapi.LeavesToAvail = teacher.LeavesToAvail;
                    teacherapi.Salary        = teacher.Salary;
                    teacherapi.Status        = teacher.Status;
                    teacherapi.Email         = teacher.CurrentUser.Email;
                    teacherapi.Password      = teacher.CurrentUser.Password;
                    return(Json(teacherapi));

                case 4:
                    Parent         parent    = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                    ParentModelApi parentapi = new ParentModelApi();
                    parentapi.ParentID     = parent.ParentID;
                    parentapi.Name         = parent.Name;
                    parentapi.HouseNo      = parent.HouseNo;
                    parentapi.BuildingName = parent.BuildingName;
                    parentapi.Street       = parent.Street;
                    parentapi.Area         = parent.Area;
                    parentapi.Location     = parent.Location;
                    parentapi.City         = parent.CurrentCity.Name;
                    parentapi.Country      = parent.CurrentCountry.Name;
                    parentapi.PinCode      = parent.PinCode;
                    parentapi.Landline     = parent.Landline;
                    parentapi.Mobile       = parent.Mobile;
                    parentapi.Photo        = parent.Photo;
                    parentapi.School       = parent.CurrentSchool.Name;
                    parentapi.Status       = parent.Status;
                    parentapi.Email        = parent.CurrentUser.Email;
                    parentapi.Password     = parent.CurrentUser.Password;
                    return(Json(parentapi));

                case 5:
                    Student         student    = db.Students.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentClass).Include(s => s.CurrentSection).Include(s => s.CurrentParent).Include(s => s.CurrentParent.CurrentUser).Include(s => s.CurrentParent.CurrentCity).Include(s => s.CurrentParent.CurrentCountry).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                    StudentModelApi studentapi = new StudentModelApi();
                    studentapi.StudentID          = student.StudentID;
                    studentapi.Name               = student.Name;
                    studentapi.IdCard             = student.IdCard;
                    studentapi.Dob                = student.Dob;
                    studentapi.Mobile             = student.Mobile;
                    studentapi.Class              = student.CurrentClass.Name;
                    studentapi.Section            = student.CurrentSection.Name;
                    studentapi.SchoolName         = student.CurrentSchool.Name;
                    studentapi.ParentName         = student.CurrentParent.Name;
                    studentapi.ParentHouseNo      = student.CurrentParent.HouseNo;
                    studentapi.ParentBuildingName = student.CurrentParent.BuildingName;
                    studentapi.ParentStreet       = student.CurrentParent.Street;
                    studentapi.ParentArea         = student.CurrentParent.Area;
                    studentapi.ParentLocation     = student.CurrentParent.Location;
                    studentapi.ParentCity         = student.CurrentParent.CurrentCity.Name;
                    studentapi.ParentCountry      = student.CurrentParent.CurrentCountry.Name;
                    studentapi.ParentPinCode      = student.CurrentParent.PinCode;
                    studentapi.ParentLandline     = student.CurrentParent.Landline;
                    studentapi.ParentMobile       = student.CurrentParent.Mobile;
                    studentapi.ParentEmail        = student.CurrentParent.CurrentUser.Email;
                    studentapi.ParentPinCode      = student.CurrentParent.PinCode;
                    studentapi.GuardianName       = student.GuardianName;
                    studentapi.GuardianMobile     = student.GuardianMobile;
                    studentapi.GuardianEmail      = student.GuardianEmail;
                    studentapi.Parent2Name        = student.Parent2Name;
                    studentapi.Parent2Mobile      = student.Parent2Mobile;
                    studentapi.Parent2Email       = student.Parent2Email;
                    studentapi.Photo              = student.Photo;
                    studentapi.Fee                = student.Fee;
                    studentapi.FeeInstalments     = student.FeeInstalments;
                    studentapi.Status             = student.Status;
                    studentapi.Email              = student.CurrentUser.Email;
                    studentapi.Password           = student.CurrentUser.Password;
                    return(Json(studentapi));

                case 6:
                    Employee         employee    = db.Employees.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                    EmployeeModelApi employeeapi = new EmployeeModelApi();
                    employeeapi.EmployeeID    = employee.EmployeeID;
                    employeeapi.Name          = employee.Name;
                    employeeapi.HouseNo       = employee.HouseNo;
                    employeeapi.BuildingName  = employee.BuildingName;
                    employeeapi.Street        = employee.Street;
                    employeeapi.Area          = employee.Area;
                    employeeapi.Location      = employee.Location;
                    employeeapi.City          = employee.CurrentCity.Name;
                    employeeapi.Country       = employee.CurrentCountry.Name;
                    employeeapi.PinCode       = employee.PinCode;
                    employeeapi.Landline      = employee.Landline;
                    employeeapi.Mobile        = employee.Mobile;
                    employeeapi.Photo         = employee.Photo;
                    employeeapi.School        = employee.CurrentSchool.Name;
                    employeeapi.LeavesToAvail = employee.LeavesToAvail;
                    employeeapi.Salary        = employee.Salary;
                    employeeapi.Status        = employee.Status;
                    employeeapi.Email         = employee.CurrentUser.Email;
                    employeeapi.Password      = employee.CurrentUser.Password;
                    return(Json(employeeapi));

                default:
                    break;
                }

                return(Json("Data is not availabe"));
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        public IHttpActionResult PutProfileApi(int id, string Name, string Photo, string Mobile)
        {
            apiheaderdata       apidata             = APIAuthorizeAttribute.GetAuthorize(Request.Headers.GetValues("Token").First());
            CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == apidata.AcademicYearID).FirstOrDefault();
            int      companyacademicyearid          = companyacademicyear.CompanyAcademicYearID;
            UserRole userrole = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == apidata.UserID).FirstOrDefault();
            int      roleid   = userrole.RoleID;
            var      schools  = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid).Where(s => s.SchoolID == 0);

            switch (roleid)
            {
            case 1:
                School school = db.Schools.Include(s => s.CurrentUser).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                school.Name            = Name;
                school.Logo            = Photo;
                school.Mobile          = Mobile;
                db.Entry(school).State = EntityState.Modified;
                db.SaveChanges();
                return(Json("Modified Successfully"));

            case 2:
                school                 = db.Schools.Include(s => s.CurrentUser).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                school.Name            = Name;
                school.Logo            = Photo;
                school.Mobile          = Mobile;
                db.Entry(school).State = EntityState.Modified;
                db.SaveChanges();
                return(Json("Modified Successfully"));

            case 3:
                Teacher teacher = db.Teachers.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                teacher.Name            = Name;
                teacher.Photo           = Photo;
                teacher.Mobile          = Mobile;
                db.Entry(teacher).State = EntityState.Modified;
                db.SaveChanges();
                break;

            case 4:
                Parent parent = db.Parents.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                parent.Name            = Name;
                parent.Photo           = Photo;
                parent.Mobile          = Mobile;
                db.Entry(parent).State = EntityState.Modified;
                db.SaveChanges();
                return(Json("Modified Successfully"));

            case 5:
                Student student = db.Students.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Include(s => s.CurrentClass).Include(s => s.CurrentSection).Include(s => s.CurrentParent).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                student.Name            = Name;
                student.Photo           = Photo;
                student.Mobile          = Mobile;
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return(Json("Modified Successfully"));

            case 6:
                Employee employee = db.Employees.Include(s => s.CurrentUser).Include(s => s.CurrentSchool).Where(s => s.UserID == apidata.UserID).FirstOrDefault();
                employee.Name            = Name;
                employee.Photo           = Photo;
                employee.Mobile          = Mobile;
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return(Json("Modified Successfully"));

            default:
                break;
            }

            return(Json("Error in Modifications"));
        }
Exemplo n.º 6
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            ViewBag.ReturnUrl      = returnUrl;
            ViewBag.AcademicYearID = new SelectList(db.AcademicYears, "AcademicYearID", "Name", model.AcademicYearID);
            ViewBag.SchoolID       = new SelectList(db.Schools, "SchoolID", "Name", model.SchoolID);
            if (ModelState.IsValid)
            {
                var userManager1 = db.Users.Where(x => x.Email == model.Email && x.Status == true).FirstOrDefault();

                if (userManager1 != null)
                {
                    string username = userManager1.Email;
                    var    user     = db.Users.Where(x => x.Email == model.Email && x.Password == model.Password).FirstOrDefault();
                    if (user != null)
                    {
                        UserRole userrole = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == userManager1.UserID).FirstOrDefault();
                        FormsAuthentication.SetAuthCookie(user.Email, false);
                        var    authTicket      = new FormsAuthenticationTicket(1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(2880), model.RememberMe, userrole.CurrentRole.Name);
                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        var    authCookie      = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        HttpContext.Response.Cookies.Add(authCookie);

                        string rolename = userrole.CurrentRole.Name;
                        int    roleid   = userrole.RoleID;

                        Company company = db.Companys.Find(1);
                        School  school  = db.Schools.Find(model.SchoolID);

                        CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == model.AcademicYearID).FirstOrDefault();

                        Session["AcademicYear"]          = companyacademicyear.CurrentAcademicYear.Name;
                        Session["AcademicYearID"]        = companyacademicyear.CurrentAcademicYear.AcademicYearID;
                        Session["CompanyAcademicYearID"] = companyacademicyear.CompanyAcademicYearID;

                        Session["Email"]            = user.Email;
                        Session["UserID"]           = user.UserID.ToString();
                        Session["Role"]             = rolename;
                        Session["RoleID"]           = roleid;
                        Session["Company"]          = company.Name;
                        Session["CompanyLogo"]      = company.Logo;
                        Session["SchoolID"]         = school.SchoolID;
                        Session["School"]           = school.Name;
                        Session["AcademicYearList"] = db.AcademicYears.ToList();


                        Session["CanCreate"] = user.CanCreate;
                        Session["CanEdit"]   = user.CanEdit;
                        Session["CanDelete"] = user.CanDelete;
                        Session["CanPrint"]  = user.CanPrint;

                        int SchoolsCount = db.Schools.Count();
                        Session["SchoolsCount"] = SchoolsCount;
                        int TeachersCount = db.Teachers.Count();
                        Session["TeachersCount"] = TeachersCount;
                        int ParentsCount = db.Parents.Count();
                        Session["ParentsCount"] = ParentsCount;
                        int StudentsCount = db.Students.Count();
                        Session["StudentsCount"] = StudentsCount;
                        int EmployeesCount = db.Employees.Count();
                        Session["EmployeesCount"] = EmployeesCount;

                        try
                        {
                            bool             idDefautLanguage = company.DefaultLanguage;
                            List <MenuModel> menuList         = new List <MenuModel>();
                            var submenuitems = db.SubMenus.Where(s => s.SubMenuStatus == true).OrderBy(s => s.SubMenuOrder);

                            foreach (SubMenu sm in submenuitems.ToList())
                            {
                                string[] rolesarray = sm.SubMenuRoles.Split(',');
                                if (rolesarray[0] == "true" && roleid == 1)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }
                                if (rolesarray[1] == "true" && roleid == 2)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }
                                if (rolesarray[2] == "true" && roleid == 3)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }
                                if (rolesarray[3] == "true" && roleid == 4)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }

                                if (rolesarray[4] == "true" && roleid == 5)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }
                                if (rolesarray[5] == "true" && roleid == 6)
                                {
                                    MainMenu  mainmenu = db.MainMenus.Find(sm.MainMenuID);
                                    MenuModel mm       = new MenuModel();
                                    mm.MainMenuID = sm.MainMenuID;
                                    if (idDefautLanguage)
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName;
                                        mm.SubMenuName  = sm.SubMenuName;
                                    }
                                    else
                                    {
                                        mm.MainMenuName = mainmenu.MainMenuName2;
                                        mm.SubMenuName  = sm.SubMenuName2;
                                    }
                                    mm.SubMenuID      = sm.SubMenuID;
                                    mm.ControllerName = sm.Controller;
                                    mm.ActionName     = sm.Action;
                                    mm.RoleID         = roleid;
                                    mm.RoleName       = rolename;
                                    menuList.Add(mm);
                                }
                            }
                            Session["Menu"]      = menuList;
                            Session["Dashboard"] = "/dashboard";

                            user.LastLoggedOn    = DateTime.Now;
                            db.Entry(user).State = EntityState.Modified;
                            db.SaveChanges();

                            if (companyacademicyear.CurrentAcademicYear.AcademicYearID == 1)
                            {
                                return(RedirectToAction("Index", "dashboard"));
                            }
                            else
                            {
                                return(RedirectToAction("Index", "dashboard", new { id = companyacademicyear.CurrentAcademicYear.AcademicYearID }));
                            }
                        }
                        catch
                        {
                            ModelState.AddModelError("", "Error in retrieve menu items, please contact admin.");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid username or password.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            return(View(model));
        }
        // GET api/School
        public IHttpActionResult GetSchools()
        {
            //var schoollist = db.Schools.ToJsonArray();
            //var response = Request.CreateResponse(HttpStatusCode.OK);
            //response.Content = new StringContent(Json(schoollist), Encoding.UTF8, "application/json");
            try
            {
                apiheaderdata apidata = APIAuthorizeAttribute.GetAuthorize(Request.Headers.GetValues("Token").First());
                //var encodedString = Request.Headers.GetValues("Token").First();
                //apiheaderdata apidata = new apiheaderdata();
                //if (!string.IsNullOrEmpty(encodedString))
                //{
                //    var key = EncryptionLibrary.DecryptText(encodedString);
                //    string[] parts = key.Split(new char[] { ':' });
                //    apidata.UserID = Convert.ToInt32(parts[0]);       // UserID
                //    apidata.SchoolID = Convert.ToInt32(parts[2]);    // SchoolID
                //    apidata.AcademicYearID = Convert.ToInt32(parts[3]);    // AcademicYearID

                //}
                CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == apidata.AcademicYearID).FirstOrDefault();
                int companyacademicyearid = companyacademicyear.CompanyAcademicYearID;
                var userroles             = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == apidata.UserID).FirstOrDefault();
                int roleid  = userroles.CurrentRole.RoleID;
                var schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid).Where(s => s.SchoolID == 0);
                switch (roleid)
                {
                case 1:
                    schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid).OrderBy(s => s.Name);
                    break;

                case 2:
                    schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == apidata.SchoolID).OrderBy(s => s.Name);
                    break;

                case 3:
                    schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == apidata.SchoolID).OrderBy(s => s.Name);
                    break;

                case 4:
                    schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == apidata.SchoolID).OrderBy(s => s.Name);
                    break;

                case 5:
                    schools = db.Schools.Include(s => s.CurrentUser).Include(s => s.CurrentCompanyAcademicYear).Include(s => s.CurrentCity).Include(s => s.CurrentCountry).Where(s => s.CurrentCompanyAcademicYear.CompanyAcademicYearID == companyacademicyearid && s.SchoolID == apidata.SchoolID).OrderBy(s => s.Name);
                    break;

                default:
                    break;
                }
                List <School> SchoolList = new List <School>();
                foreach (School schoolmodel in schools)
                {
                    School school = new School();
                    school.SchoolID              = schoolmodel.SchoolID;
                    school.Name                  = schoolmodel.Name;
                    school.ShortName             = schoolmodel.ShortName;
                    school.HouseNo               = schoolmodel.HouseNo;
                    school.BuildingName          = schoolmodel.BuildingName;
                    school.Street                = schoolmodel.Street;
                    school.Area                  = schoolmodel.Area;
                    school.Location              = schoolmodel.Location;
                    school.CityID                = schoolmodel.CityID;
                    school.CountryID             = schoolmodel.CountryID;
                    school.PinCode               = schoolmodel.PinCode;
                    school.Landline              = schoolmodel.Landline;
                    school.Mobile                = schoolmodel.Mobile;
                    school.Website               = schoolmodel.Website;
                    school.RegNo                 = schoolmodel.RegNo;
                    school.Logo                  = schoolmodel.Logo;
                    school.Principal             = schoolmodel.Principal;
                    school.UserID                = schoolmodel.UserID;
                    school.CompanyAcademicYearID = schoolmodel.CompanyAcademicYearID;
                    school.Status                = schoolmodel.Status;
                    SchoolList.Add(school);
                }

                return(Json(SchoolList.ToList()));
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        public IHttpActionResult PostSchool(SchoolModel schoolmodel, string newlogo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            User schoolold = db.Users.Where(s => s.Email == schoolmodel.Email).FirstOrDefault();

            if (schoolold != null)
            {
                return(Json("Email already exists"));
            }
            else
            {
                if (newlogo != "")
                {
                    schoolmodel.Logo = newlogo;
                }

                try
                {
                    apiheaderdata apidata        = APIAuthorizeAttribute.GetAuthorize(Request.Headers.GetValues("Token").First());
                    User          superadminuser = db.Users.Find(1);

                    User user = new User();
                    user.Email        = schoolmodel.Email;
                    user.Password     = schoolmodel.Password;
                    user.CanCreate    = false;
                    user.CanEdit      = false;
                    user.CanDelete    = false;
                    user.CanPrint     = false;
                    user.Status       = false;
                    user.CreatedOn    = DateTime.Now;
                    user.LastLoggedOn = null;
                    db.Users.Add(user);
                    db.SaveChanges();

                    Role     role      = db.Roles.Where(r => r.Name == "SchoolAdmin").FirstOrDefault();
                    UserRole userroles = new UserRole();
                    userroles.UserID = user.UserID;
                    userroles.RoleID = role.RoleID;
                    db.UserRoles.Add(userroles);
                    db.SaveChanges();

                    School school = new School();
                    school.Name                  = schoolmodel.Name;
                    school.ShortName             = schoolmodel.ShortName;
                    school.HouseNo               = schoolmodel.HouseNo;
                    school.BuildingName          = schoolmodel.BuildingName;
                    school.Street                = schoolmodel.Street;
                    school.Area                  = schoolmodel.Area;
                    school.Location              = schoolmodel.Location;
                    school.CityID                = schoolmodel.CityID;
                    school.CountryID             = schoolmodel.CountryID;
                    school.PinCode               = schoolmodel.PinCode;
                    school.Landline              = schoolmodel.Landline;
                    school.Mobile                = schoolmodel.Mobile;
                    school.Website               = schoolmodel.Website;
                    school.RegNo                 = schoolmodel.RegNo;
                    school.Logo                  = schoolmodel.Logo;
                    school.CompanyAcademicYearID = schoolmodel.CompanyAcademicYearID;
                    school.Principal             = schoolmodel.Principal;
                    school.UserID                = user.UserID;
                    CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == apidata.AcademicYearID).FirstOrDefault();
                    school.CompanyAcademicYearID = companyacademicyear.CompanyAcademicYearID;
                    school.Status = schoolmodel.Status;
                    db.Schools.Add(school);
                    db.SaveChanges();

                    user.Status          = true;
                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();

                    string To = schoolmodel.Email, WebsiteUrl, Ssl, UserID, Password, SMTPPort, Host;
                    CommanFunctions.AppSettings(out WebsiteUrl, out Ssl, out UserID, out Password, out SMTPPort, out Host);
                    string subject = "New School Created";
                    string body    = "Hi,";
                    body += "<br/> School was created. Please login using these credentials, and update school profile. <br/> Login email : " + schoolmodel.Email;
                    body += "<br/> Login password : "******"<br/> <br/> ----------------------";
                    body += "<br/> Admin";
                    body += "<br/> <a href=" + WebsiteUrl + ">" + WebsiteUrl + "</a>";
                    try { CommanFunctions.SendEmail(UserID, subject, body, superadminuser.Email, To, Ssl, UserID, Password, SMTPPort, Host); }
                    catch { }
                }
                catch
                {
                    return(Json("Error in creating"));
                }
            }

            return(Json("Created Successfully"));
        }
Exemplo n.º 9
0
        // POST: api/Authenticate
        public HttpResponseMessage Authenticate(LoginViewModel model)
        {
            Token token1 = new Token();

            model.AcademicYearID = 1;
            model.SchoolID       = 1;
            if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && model.AcademicYearID > 0 && model.SchoolID > 0)
            {
                var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
                response         = Request.CreateResponse(HttpStatusCode.OK, "Required Email, Password, AcademicYearID, SchoolID");
                response.Content = new StringContent("Not Valid Request");
                return(response);
            }
            else
            {
                var userManager1 = db.Users.Where(x => x.Email == model.Email && x.Status == true).FirstOrDefault();
                if (userManager1 != null)
                {
                    string username = userManager1.Email;
                    var    user     = db.Users.Where(x => x.Email == model.Email && x.Password == model.Password).FirstOrDefault();
                    if (user != null)
                    {
                        UserRole            userrole            = db.UserRoles.Include(x => x.CurrentRole).Where(x => x.UserID == userManager1.UserID).FirstOrDefault();
                        string              rolename            = userrole.CurrentRole.Name;
                        int                 roleid              = userrole.RoleID;
                        Company             company             = db.Companys.Find(1);
                        School              school              = db.Schools.Find(model.SchoolID);
                        CompanyAcademicYear companyacademicyear = db.CompanyAcademicYears.Include(c => c.CurrentCompany).Include(c => c.CurrentAcademicYear).Where(c => c.AcademicYearID == model.AcademicYearID).FirstOrDefault();
                        var                 key          = KeyGenerator.GetUniqueKey();
                        string              randomnumber =
                            string.Join(":", new string[]
                        {
                            key,
                            Convert.ToString(userManager1.UserID),
                            Convert.ToString(roleid),
                            Convert.ToString(school.SchoolID),
                            Convert.ToString(companyacademicyear.CurrentAcademicYear.AcademicYearID),
                            Convert.ToString(DateTime.Now.Ticks)
                        });
                        var newToken = EncryptionLibrary.EncryptText(randomnumber);

                        //var tokensexpired = db.Tokens.Where(t => t.ExpiresOn <= DateTime.Now);
                        //foreach(Token tokenexpired in tokensexpired)
                        //{
                        //    db.Tokens.Remove(tokenexpired);
                        //    db.SaveChanges();
                        //}

                        token1.Password       = "";
                        token1.TokenKey       = key;
                        token1.IssuedOn       = DateTime.Now;
                        token1.ExpiresOn      = DateTime.Now.AddMinutes(Convert.ToInt32(ConfigurationManager.AppSettings["TokenExpiry"]));
                        token1.CreatedOn      = DateTime.Now;
                        token1.Email          = user.Email;
                        token1.UserID         = user.UserID;
                        token1.RoleID         = roleid;
                        token1.SchoolID       = school.SchoolID;
                        token1.AcademicYearID = companyacademicyear.CurrentAcademicYear.AcademicYearID;
                        token1.Company        = company.Name;
                        token1.CompanyLogo    = company.Logo;
                        db.Tokens.Add(token1);
                        try
                        {
                            db.SaveChanges();
                            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, token1);
                            response.Headers.Add("Token", newToken);
                            response.Headers.Add("TokenExpiry", ConfigurationManager.AppSettings["TokenExpiry"]);
                            response.Headers.Add("Access-Control-Expose-Headers", "Token,TokenExpiry");
                            return(response);

                            //HttpResponseMessage response = new HttpResponseMessage();
                            //response = Request.CreateResponse(HttpStatusCode.OK, "Authorized");
                            //response.Headers.Add("Token", newToken);
                            //response.Headers.Add("TokenExpiry", ConfigurationManager.AppSettings["TokenExpiry"]);
                            //response.Headers.Add("Access-Control-Expose-Headers", "Token,TokenExpiry");
                            //response.Content = token;
                            //return response;
                            //return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(token, System.Text.Encoding.UTF8, "application/json") };
                        }
                        catch
                        {
                            var response = new HttpResponseMessage(HttpStatusCode.NotFound);
                            response         = Request.CreateResponse(HttpStatusCode.OK, "Wrong Password");
                            response.Content = new StringContent("Wrong Password");
                            return(response);
                        }
                    }
                    else
                    {
                        var response = new HttpResponseMessage(HttpStatusCode.NotFound);
                        response         = Request.CreateResponse(HttpStatusCode.OK, "Wrong Password");
                        response.Content = new StringContent("Wrong Password");
                        return(response);
                    }
                }
                else
                {
                    //var message = new HttpResponseMessage(HttpStatusCode.NotFound);
                    //message.Content = new StringContent("Email is not available");
                    //return new HttpResponseMessage { StatusCode = HttpStatusCode.NotAcceptable };
                    var response = new HttpResponseMessage(HttpStatusCode.NotFound);
                    response         = Request.CreateResponse(HttpStatusCode.OK, "Email is not available");
                    response.Content = new StringContent("Email is not available");
                    return(response);
                }
            }

            //if (string.IsNullOrEmpty(ClientKeys.ClientID) && string.IsNullOrEmpty(ClientKeys.ClientSecret))
            //{
            //    var message = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
            //    message.Content = new StringContent("Not Valid Request");
            //    return message;
            //}
            //else
            //{
            //    if (_IAuthenticate.ValidateKeys(ClientKeys))
            //    {
            //        var clientkeys = _IAuthenticate.GetClientKeysDetailsbyCLientIDandClientSecert(ClientKeys.ClientID, ClientKeys.ClientSecret);

            //        if (clientkeys == null)
            //        {
            //            var message = new HttpResponseMessage(HttpStatusCode.NotFound);
            //            message.Content = new StringContent("InValid Keys");
            //            return message;
            //        }
            //        else
            //        {
            //            if (_IAuthenticate.IsTokenAlreadyExists(clientkeys.CompanyID))
            //            {
            //                _IAuthenticate.DeleteGenerateToken(clientkeys.CompanyID);

            //                return GenerateandSaveToken(clientkeys);
            //            }
            //            else
            //            {
            //                return GenerateandSaveToken(clientkeys);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        var message = new HttpResponseMessage(HttpStatusCode.NotFound);
            //        message.Content = new StringContent("InValid Keys");
            //        return new HttpResponseMessage { StatusCode = HttpStatusCode.NotAcceptable };
            //    }
            //}
        }