private List <Role> GetUserRoles(ApplicationUser user, TelesalesScheduleDbContext context)
        {
            // Create user manager
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            // Get all application roles
            var roles = context.Roles
                        .Select(r => r.Name)
                        .OrderBy(r => r)
                        .ToList();

            // For each application role, check if the user has it
            var userRoles = new List <Role>();

            foreach (var roleName in roles)
            {
                var role = new Role {
                    Name = roleName
                };

                if (userManager.IsInRole(user.Id, roleName))
                {
                    role.IsSelected = true;
                }

                userRoles.Add(role);
            }

            // Return a list with all roles
            return(userRoles);
        }
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get user from database
                var user = context.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                // when deleting user to change IsDeleted = true in Employee
                var employee = context.Employees.SingleOrDefault(e => e.UserName == user.UserName);
                employee.IsDeleted            = true;
                context.Entry(employee).State = EntityState.Modified;

                // Delete user and save changes
                context.Users.Remove(user);
                context.SaveChanges();

                this.AddNotification("User deleted.", NotificationType.WARNING);

                return(RedirectToAction("List"));
            }
        }
Пример #3
0
        public ActionResult Edit(EditEmployeeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    var employee = context.Employees
                                   .FirstOrDefault(e => e.Id == viewModel.Id);

                    if (employee == null)
                    {
                        return(HttpNotFound());
                    }

                    employee.FullName         = viewModel.FullName;
                    employee.UserName         = viewModel.UserName;
                    employee.Manager          = context.Employees.FirstOrDefault(m => m.FullName == viewModel.ManagerFullName);
                    employee.BirthDay         = viewModel.BirthDay;
                    employee.FullTimeAgent    = viewModel.FullTimeAgent;
                    employee.SaveDeskAgent    = viewModel.SaveDeskAgent;
                    employee.SeniorSpecialist = viewModel.SeniorSpecialist;
                    employee.IsDeleted        = viewModel.IsDeleted;

                    context.Entry(employee).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Employee edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
        //
        // GET: User/Edit
        public ActionResult Edit(string id)
        {
            // Validate id
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get user from database
                var user = context.Users
                           .Where(u => u.Id == id)
                           .First();

                // Check if user exists
                if (user == null)
                {
                    return(HttpNotFound());
                }

                // Create a view model
                var viewModel = new EditUserViewModel();
                viewModel.User  = user;
                viewModel.Roles = GetUserRoles(user, context);

                // Pass the model to the view
                return(View(viewModel));
            }
        }
Пример #5
0
        //
        // GET: Computer/List
        public ActionResult List()
        {
            using (var context = new TelesalesScheduleDbContext())
            {
                var computers = context.Computers.ToList();

                return(View(computers));
            }
        }
Пример #6
0
        //
        // GET: Employee/List
        public ActionResult List()
        {
            using (var context = new TelesalesScheduleDbContext())
            {
                var employees = context.Employees
                                .Include(e => e.Manager)
                                .ToList();

                return(View(employees));
            }
        }
        //
        // GET: User/List
        public ActionResult List()
        {
            using (var context = new TelesalesScheduleDbContext())
            {
                var users = context.Users.ToList();

                // Get all admins and store them for the view, using ViewBag
                var admins = GetAdminUserNames(users, context);
                ViewBag.Admins = admins;

                return(View(users));
            }
        }
Пример #8
0
        public ActionResult Edit(Computer computer)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    context.Entry(computer).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Computer edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(computer));
        }
Пример #9
0
        public ActionResult Create(Computer computer)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    context.Computers.Add(computer);
                    context.SaveChanges();

                    this.AddNotification("Computer created.", NotificationType.SUCCESS);

                    return(RedirectToAction("List"));
                }
            }

            return(View(computer));
        }
Пример #10
0
        private HashSet <string> GetAdminUserNames(List <ApplicationUser> users, TelesalesScheduleDbContext context)
        {
            // Create user manager
            var userManager = new UserManager <ApplicationUser>(
                new UserStore <ApplicationUser>(context));

            var admins = new HashSet <string>();

            // Get all usernames of users with role Admin
            foreach (var user in users)
            {
                if (userManager.IsInRole(user.Id, "Admin"))
                {
                    admins.Add(user.UserName);
                }
            }

            return(admins);
        }
Пример #11
0
        private void SetUserRoles(EditUserViewModel viewModel, ApplicationUser user, TelesalesScheduleDbContext context)
        {
            // Create user manager
            var userManager = HttpContext.GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            // for each role checks weather it is selected and if the user is in the role
            foreach (var role in viewModel.Roles)
            {
                if (role.IsSelected && !userManager.IsInRole(user.Id, role.Name))
                {
                    userManager.AddToRole(user.Id, role.Name);
                }
                else if (!role.IsSelected && userManager.IsInRole(user.Id, role.Name))
                {
                    userManager.RemoveFromRole(user.Id, role.Name);
                }
            }
        }
Пример #12
0
        //
        // GET: Computer/Edit
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                var computer = context.Computers
                               .FirstOrDefault(c => c.Id == id);

                if (computer == null)
                {
                    return(HttpNotFound());
                }

                return(View(computer));
            }
        }
Пример #13
0
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    // Get user from database
                    var user = context.Users.FirstOrDefault(u => u.Id == id);

                    // Check if user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    // If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    // Set user properties
                    user.Email    = viewModel.User.Email;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, context);

                    // Save changes
                    context.Entry(user).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("User edited", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
Пример #14
0
        public ActionResult NextWeek()
        {
            using (var db = new TelesalesScheduleDbContext())
            {
                DateTime monday = DateTime.Now;

                while (monday.DayOfWeek != DayOfWeek.Monday)
                {
                    monday = monday.AddDays(1);
                }
                var sunday = monday.AddDays(6);

                ViewBag.StartDate = monday.Date.ToShortDateString();
                ViewBag.EndDate   = sunday.Date.ToShortDateString();
                var computers = db.Schedules.Where(s => s.StartDate == monday.Date && s.EndDate == sunday.Date).Select(p => p.Computer).ToList();

                var comps = db.Schedules.Where(s => s.StartDate == monday.Date && s.EndDate == sunday.Date).Where(p => p.Computer != null && p.Computer.IsWorking == true).ToList();

                return(View(comps));
            }
        }
Пример #15
0
        //
        // GET: Employee/Edit
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get employee from database
                var employee = context.Employees
                               .Where(e => e.Id == id)
                               .First();

                // Check if employee exists
                if (employee == null)
                {
                    return(HttpNotFound());
                }

                var viewModel = new EditEmployeeViewModel();
                viewModel.Id               = employee.Id;
                viewModel.FullName         = employee.FullName;
                viewModel.UserName         = employee.UserName;
                viewModel.ManagerId        = employee.ManagerId;
                viewModel.ManagerFullName  = employee.Manager == null ? null : employee.Manager.FullName;
                viewModel.BirthDay         = employee.BirthDay;
                viewModel.FullTimeAgent    = employee.FullTimeAgent;
                viewModel.SaveDeskAgent    = employee.SaveDeskAgent;
                viewModel.SeniorSpecialist = employee.SeniorSpecialist;
                viewModel.IsDeleted        = employee.IsDeleted;

                if (employee.Manager != null)
                {
                    viewModel.ManagerFullName = employee.Manager.FullName;
                }

                return(View(viewModel));
            }
        }
Пример #16
0
        public ActionResult Create(EditEmployeeViewModel employee)
        {
            if (ModelState.IsValid)
            {
                using (var db = new TelesalesScheduleDbContext())
                {
                    var emoloyees = db.Employees.Select(e => e.FullName).ToList();
                    if (emoloyees.Contains(employee.FullName))
                    {
                        ViewBag.ErrorMessage = "Employee already exist!";
                        return(View());
                    }

                    else
                    {
                        var emp = new Employee
                        {
                            FullName         = employee.FullName,
                            BirthDay         = Convert.ToDateTime(employee.BirthDay),
                            FullTimeAgent    = employee.FullTimeAgent,
                            IsDeleted        = employee.IsDeleted,
                            SaveDeskAgent    = employee.SaveDeskAgent,
                            UserName         = employee.UserName,
                            SeniorSpecialist = employee.SeniorSpecialist,
                            Manager          = db.Employees.Where(e => e.FullName == employee.ManagerFullName).FirstOrDefault()
                        };

                        db.Employees.Add(emp);
                        db.SaveChanges();

                        this.AddNotification("Employee created.", NotificationType.SUCCESS);
                    }

                    return(RedirectToAction("List"));
                }
            }

            return(View(employee));
        }
Пример #17
0
        //
        // GET: User/Delete
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get user from database
                var user = context.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                // Check if user exists
                if (user == null)
                {
                    return(HttpNotFound());
                }

                return(View(user));
            }
        }