示例#1
0
 public ActionResult Edit([Bind(Include = "DepartmentId,DepartmentName,Domain")] Department department)
 {
     try
     {
         //only super user can edit departments
         if (IsSuperUser())
         {
             using (var db = new CAMS_DatabaseEntities())
             {
                 if (ModelState.IsValid)
                 {
                     db.Entry(department).State = EntityState.Modified;
                     db.SaveChanges();
                     return(RedirectToAction("Index"));
                 }
                 return(View(department));
             }
         }
         return(RedirectAcordingToLogin());
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#2
0
 public int GetComputerId(string v)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Computers.Where(e => e.ComputerName.ToLower() == v.ToLower()).Select(e => e.ComputerId).FirstOrDefault());
     }
 }
示例#3
0
        public ActivityType CurrentActivityDetails(int id)
        {
            Computer comp;

            using (var db = new CAMS_DatabaseEntities())
            {
                comp = db.Computers.Find(id);


                if (comp == null)
                {
                    return(ActivityType.On);
                }
                List <Activity> activities;
                //  lock (db)
                {
                    activities = comp.Activities.Select(e => e).Where(e => e.Logout.Equals(null)).ToList();
                }
                if (activities.Count() == 0)
                {
                    return(ActivityType.On);
                }
                return(activities.Last().Mode);
            }
        }
示例#4
0
        // GET: Labs/Create
        public ActionResult Create()
        {
            try
            {
                if (IsFullAccessUser())
                {
                    using (var db = new CAMS_DatabaseEntities())
                    {
                        //Tuple<List<Department>, List<string>> DepartmentsAndBuildings;
                        List <Department> departments     = db.Departments.ToList();
                        List <Department> userDepartments = new List <Department>();
                        foreach (var item in departments)
                        {
                            //user can only add labs to the departments he have FULL accsses to.
                            if (IsFullAccess(item.DepartmentId))
                            {
                                userDepartments.Add(item);
                            }
                        }

                        List <string> buildings = db.Labs.Select(lab => lab.Building).Distinct().ToList();
                        return(View(new object[] { userDepartments, buildings }));
                    }
                }
                else
                {
                    return(RedirectAcordingToLogin());
                }
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
示例#5
0
 internal List <string> GetBuildings()
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Labs.Select(x => x.Building).ToList());
     }
 }
示例#6
0
        // GET: Labs/Edit/5
        public ActionResult Edit(int?id)
        {
            try
            {
                if (id == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }
                using (var db = new CAMS_DatabaseEntities())
                {
                    Lab lab = db.Labs.Find(id);
                    if (lab == null)
                    {
                        return(HttpNotFound());
                    }
                    if (IsLimitedAccess(lab.DepartmentId))
                    {
                        db.Entry(lab).Collection(e => e.Computers).Load();
                        db.Entry(lab).Reference(e => e.Department).Load();

                        ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", lab.DepartmentId);
                        return(View(new LabDetailsViewModel(lab, this)));
                    }
                    //user have no access to edit lab
                    else
                    {
                        return(RedirectAcordingToLogin());
                    }
                }
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
示例#7
0
 // GET: Labs/Delete/5
 public ActionResult Delete(int?id)
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             if (id == null)
             {
                 return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
             }
             Lab lab = db.Labs.Find(id);
             if (IsFullAccess(lab.DepartmentId))
             {
                 db.Entry(lab).Reference(e => e.Department).Load();
                 if (lab == null)
                 {
                     return(HttpNotFound());
                 }
                 return(View(lab));
             }
             //user have no access to delete lab
             else
             {
                 return(RedirectAcordingToLogin());
             }
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#8
0
 internal DateTime?GetLastLogOutTime(int computerId)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Computers.Find(computerId).Activities.Where(e => e.Mode == ActivityType.User).Max(e => e.Logout));
     }
 }
示例#9
0
 // GET: Notifications
 public ActionResult Index()
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             int userId = GetConnectedUser();
             if (userId != -1)
             {
                 User user = db.Users.Find(userId);
                 if (user != null)
                 {
                     return(View(new NotificationViewModel(user, this)));
                 }
                 return(RedirectToAction("Login", "Account"));
             }
             else
             {
                 return(RedirectAcordingToLogin());
             }
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#10
0
 internal Activity GetUserActivity(int computerId)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Computers.Find(computerId).Activities.Where(e => !e.Logout.HasValue && e.Mode == ActivityType.User).ToList().FirstOrDefault());
     }
 }
示例#11
0
 public ActionResult Edit(FormCollection collection)
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             int userId = GetConnectedUser();
             if (userId != -1)
             {
                 User user = db.Users.Find(userId);
                 user.DisconnectedPeriod    = Convert.ToInt32(Request["DisconnectedPeriod"].ToString());
                 user.NotActivePeriod       = Convert.ToInt32(Request["NotActivePeriod"].ToString());
                 user.NotificationFrequency = (NotificationFrequency)Convert.ToByte(Request["NotificationFrequency"].ToString());
                 db.Entry(user).State       = EntityState.Modified;
                 db.SaveChanges();
                 return(RedirectToAction("Index"));
             }
             return(RedirectAcordingToLogin());
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#12
0
        public ActionResult Create([Bind(Include = "DepartmentName,Domain")] Department department)
        {
            try
            {
                //only super user can create departments
                if (IsSuperUser())
                {
                    using (var db = new CAMS_DatabaseEntities())
                    {
                        if (ModelState.IsValid)
                        {
                            db.Departments.Add(department);
                            db.SaveChanges();
                            UserDepartment userDepartment = new UserDepartment
                            {
                                UserId       = GetConnectedUser(),
                                DepartmentId = department.DepartmentId,
                                AccessType   = AccessType.Full
                            };
                            db.UserDepartments.Add(userDepartment);
                            db.SaveChanges();
                            ((Dictionary <int, AccessType>)Session["Accesses"]).Add(userDepartment.DepartmentId, userDepartment.AccessType);
                            return(RedirectToAction("Index"));
                        }

                        return(View(department));
                    }
                }
                return(RedirectAcordingToLogin());
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
示例#13
0
 // GET: Departments/Details/5
 public ActionResult Details(int?id)
 {
     try
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         //only super user can view departments
         if (IsSuperUser())
         {
             using (var db = new CAMS_DatabaseEntities())
             {
                 Department department = db.Departments.Find(id);
                 if (department == null || !IsFullAccess(department.DepartmentId))
                 {
                     return(HttpNotFound());
                 }
                 return(View(department));
             }
         }
         return(RedirectAcordingToLogin());
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#14
0
 public ActionResult DeleteConfirmed(int id)
 {
     try
     {
         //only super user can delete departments
         if (IsSuperUser())
         {
             using (var db = new CAMS_DatabaseEntities())
             {
                 Department department = db.Departments.Find(id);
                 List <int> labsId     = department.Labs.Select(e => e.LabId).ToList();
                 foreach (var lbid in labsId)
                 {
                     DeleteLab(lbid);
                 }
                 db.Departments.Remove(department);
                 try
                 {
                     db.SaveChanges();
                 }
                 catch
                 {
                     return(DeleteConfirmed(id));
                 }
                 return(RedirectToAction("Index"));
             }
         }
         return(RedirectAcordingToLogin());
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#15
0
 /// <summary>
 /// list of computers name that are allready in labs
 /// </summary>
 /// <returns></returns>
 internal List <string> ComputersInLabs()
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Computers.Where(computer => !computer.CurrentLab.Equals(null)).Select(computer => computer.ComputerName).ToList());
     }
 }
示例#16
0
 public ActionResult Edit(string NewName, string OldName)
 {
     try
     {
         if (NewName == null || NewName == string.Empty || OldName == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         using (var db = new CAMS_DatabaseEntities())
         {
             //only super user can edit buildings
             if (IsSuperUser())
             {
                 List <int> labsId = db.Labs.Where(e => e.Building.Equals(OldName)).Select(e => e.LabId).ToList();
                 foreach (var lbid in labsId)
                 {
                     UpdateLabBuilding(lbid, NewName);
                 }
                 object buildingName = NewName;
                 return(RedirectToAction("Index"));
             }
             return(RedirectAcordingToLogin());
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#17
0
        public ActionResult Create([Bind(Include = "Building,RoomNumber,DepartmentId")] Lab lab)
        {
            try
            {
                using (var db = new CAMS_DatabaseEntities())
                {
                    if (IsFullAccess(lab.DepartmentId))
                    {
                        if (ModelState.IsValid)
                        {
                            lab.ComputerSize = 50;
                            db.Labs.Add(lab);
                            db.SaveChanges();
                            return(RedirectToAction("Index"));
                        }

                        ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", lab.DepartmentId);
                        return(View(lab));
                    }
                    else
                    {
                        return(RedirectAcordingToLogin());
                    }
                }
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
示例#18
0
        private void CreateSessionProperties(string userName)
        {
            Dictionary <int, AccessType> Accesses = new Dictionary <int, AccessType>();
            User user;
            int  user_id = 0;

            using (var db = new CAMS_DatabaseEntities())
            {
                try
                {
                    user = db.Users.Where(u => u.Email.StartsWith(userName + "@")).First();
                    foreach (UserDepartment dep in user.UserDepartments)
                    {
                        Accesses.Add(dep.DepartmentId, dep.AccessType);
                    }
                    user_id = user.UserId;
                }
                catch {
                    System.Diagnostics.Debug.WriteLine("Cant fined user");
                }
            }

            Session["UserId"]     = user_id;
            Session["Accesses"]   = Accesses;
            Session["SupperUser"] = IsSupprUser(userName);
        }
示例#19
0
 public ActionResult Edit([Bind(Include = "LabId,TodaysClasses,Building,RoomNumber,DepartmentId,Computers")] Lab lab)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(RedirectToAction("Index"));
     }
 }
示例#20
0
 // GET: Users
 public ActionResult Index(bool?byDepartment)
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             if (byDepartment.HasValue)
             {
                 ViewBag.byDepartment = byDepartment.Value;
             }
             else
             {
                 ViewBag.byDepartment = false;
             }
             int userId = GetConnectedUser();
             if (userId != -1)
             {
                 User user = db.Users.Find(userId);
                 if (user == null)
                 {
                     return(HttpNotFound());
                 }
                 db.Entry(user).Collection(e => e.UserDepartments).Load();
                 return(View(new AccessViewModel(user, this, ViewBag.byDepartment)));
             }
             return(RedirectAcordingToLogin());
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#21
0
        public bool SaveLabEdit(List <int> comps, int labId, string roomNumber, int ComputerSize)
        {
            using (var db = new CAMS_DatabaseEntities())
            {
                Lab lab = db.Labs.Find(labId);

                lab.RoomNumber   = roomNumber;
                lab.ComputerSize = ComputerSize;
                var privLabComputers = lab.Computers.Select(e => e.ComputerId).ToList();
                //computers in the lab that was removed (not in the current computer list)
                foreach (var item in privLabComputers.Except(comps))
                {
                    RemoveComputerFromLab(item, lab.LabId);
                }
                //computers added to the lab (not in the lab computer list)
                foreach (var item in comps.Except(privLabComputers))
                {
                    AddComputerToLab(item, lab.LabId);
                }

                //computers to update
                //foreach (var item in comps.Intersect(privLabComputers))
                //{
                //    db.Entry(item).State = EntityState.Modified;
                //}
                db.Entry(lab).State = EntityState.Modified;
                db.SaveChanges();
                return(true);
            }
        }
示例#22
0
 // GET: Users/Create
 public ActionResult Create()
 {
     try
     {
         if (IsFullAccessUser())
         {
             using (var db = new CAMS_DatabaseEntities())
             {
                 int  userId = (int)Session["UserId"];
                 User user   = db.Users.Find(userId);
                 if (user == null)
                 {
                     return(HttpNotFound());
                 }
                 db.Entry(user).Collection(e => e.UserDepartments).Load();
                 return(View(new AccessViewModel(user, this)));
             }
         }
         return(RedirectAcordingToLogin());
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#23
0
 public List <User> GetEmailSubscribers(NotificationFrequency frequency)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Users.Where(e => e.NotificationFrequency == frequency).ToList());
     }
 }
示例#24
0
 public ActionResult Edit([Bind(Include = "UserId, DepartmentId, AccessType")] UserDepartment UserDepartment)
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             if (ModelState.IsValid)
             {
                 //only full access user for department can edit accesses to this departments
                 if (IsFullAccess(UserDepartment.DepartmentId))
                 {
                     db.Entry(UserDepartment).State = EntityState.Modified;
                     db.SaveChanges();
                     return(RedirectToAction("Index"));
                 }
                 return(RedirectAcordingToLogin());
             }
             return(View(UserDepartment));
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#25
0
 internal List <Lab> GetLabsOfDepartment(int departmentId)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Labs.Where(lab => lab.DepartmentId == departmentId).ToList());
     }
 }
示例#26
0
 // GET: Users/Delete/5
 public ActionResult Delete(int?userId, int?depId)
 {
     try
     {
         using (var db = new CAMS_DatabaseEntities())
         {
             if (userId == null || depId == null)
             {
                 return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
             }
             //only full access user for department can remove accesses to this departments
             if (IsFullAccess(depId.Value))
             {
                 UserDepartment userDep = db.UserDepartments.Where(x => x.DepartmentId == depId && x.UserId == userId).ToList().First();
                 if (userDep == null)
                 {
                     return(HttpNotFound());
                 }
                 db.Entry(userDep).Reference(e => e.User).Load();
                 db.Entry(userDep).Reference(e => e.Department).Load();
                 return(View(userDep));
             }
             return(RedirectAcordingToLogin());
         }
     }
     catch
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
示例#27
0
 internal List <Department> GetDepartments()
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Departments.ToList());
     }
 }
示例#28
0
        internal List <SelectListItem> GetUserDepartmentsListFull(int userId)
        {
            using (var db = new CAMS_DatabaseEntities())
            {
                List <SelectListItem> list = new List <SelectListItem>();
                if (IsSuperUser())
                {
                    //for super user get all departments
                    foreach (Department d in db.Departments.ToList())
                    {
                        list.Add(new SelectListItem {
                            Text = d.DepartmentName, Value = d.DepartmentId.ToString()
                        });
                    }
                }
                else
                {
                    List <UserDepartment> departments = db.UserDepartments.Where(e => e.UserId == userId && e.AccessType == AccessType.Full).ToList();
                    foreach (UserDepartment d in departments)
                    {
                        list.Add(new SelectListItem {
                            Text = d.Department.DepartmentName, Value = d.Department.DepartmentId.ToString()
                        });
                    }
                }

                return(list);
            }
        }
示例#29
0
 internal Computer GetComputer(int computerId)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         return(db.Computers.Find(computerId));
     }
 }
示例#30
0
 public void CreateNewActivity(int compId, ActivityType mode, string userName)
 {
     using (var db = new CAMS_DatabaseEntities())
     {
         // Activity act = new Activity();
         Computer comp  = db.Computers.Find(compId);
         string   uName = null;
         if (userName != null)
         {
             uName = userName;
         }
         Activity act = new Activity
         {
             UserName   = uName,
             Mode       = mode,
             Login      = DateTime.Now,
             Weekend    = IsWeekend(DateTime.Now.DayOfWeek),
             ComputerId = comp.ComputerId
         };
         db.Activities.Add(act);
         comp.Activities.Add(act);
         db.Entry(comp).State = EntityState.Modified; // is it the way to update? enother option:  db.Set<X>().AddOrUpdate(x);
         db.SaveChanges();
     }
 }