public ActionResult Create()
 {
     ViewBag.departmentID = new SelectList(db.Departments, "departmentID", "departmentName");
     User user = new User();
     user.Roles = (from role in db.Roles
                   select role).ToList();
     return View(user);
 }
 public ActionResult Create(User user)
 {
     var r = from role in db.Roles
             join userRole in user.SelectedRoleIDs on role.roleID equals userRole
             select role;
     user.Roles = r.ToList();
     db.Users.Add(user);
     db.SaveChanges();
     return RedirectToAction("Index");
 }
Exemple #3
0
 public static void RefreshCache()
 {
     using (tskmContainer db = new tskmContainer())
     {
         if (HttpContext.Current.User != null)
         {
             _user = db.Users.SingleOrDefault(x => x.username == HttpContext.Current.User.Identity.Name);
             if (_user != null)
             {
                 _user.Roles.ToList();
             }
         }
     }
 }
 public ActionResult Edit(User user)
 {
     User m_dbUser = db.Users.Single(x => x.userID == user.userID);
     m_dbUser.userFname = user.userFname;
     m_dbUser.userLname = user.userLname;
     m_dbUser.userTel = user.userTel;
     m_dbUser.userEmail = user.userEmail;
     m_dbUser.userPosition = user.userPosition;
     var r = from role in db.Roles
             join userRole in user.SelectedRoleIDs on role.roleID equals userRole
             select role;
     m_dbUser.Roles.Clear();
     m_dbUser.Roles = r.ToList();
     db.SaveChanges();
     return RedirectToAction("Index");
 }
 public ActionResult Login(User user)
 {
     var u = (from us in db.Users
              where us.username == user.username && us.password == user.password
              select us).SingleOrDefault();
     if (u != null)
     {
         FormsAuthentication.SetAuthCookie(user.username, true);
         return RedirectToAction("Menu");
     }
     else
     {
         ModelState.AddModelError("", "");
     }
     return View(user);
 }
Exemple #6
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            PostAuthenticateRequest += Application_PostAuthenticateRequest;

            using (tskmContainer db = new tskmContainer())
            {
                if (!db.Catagories.Any())
                {
                    db.Catagories.Add(new Catagory
                    {
                        catagoryName = "Hardware"
                    });
                    db.Catagories.Add(new Catagory
                    {
                        catagoryName = "Software"
                    });
                    db.Catagories.Add(new Catagory
                    {
                        catagoryName = "Others"
                    });
                }

                if (!db.Roles.Any())
                {
                    db.Roles.Add(new Role
                    {
                        Name = "Admin",
                        Description = "ผู้ดูแลระบบ"
                    });

                    db.Roles.Add(new Role
                    {
                        Name = "IT Manager",
                        Description = "ผู้จัดการแผนก IT"
                    });

                    db.Roles.Add(new Role
                    {
                        Name = "IT User",
                        Description = "เจ้าหน้าที่ IT"
                    });

                    db.Roles.Add(new Role
                    {
                        Name = "User",
                        Description = "ผู้ใช้งาน"
                    });
                }

                if (!db.Departments.Any())
                {
                    db.Departments.Add(new Department
                    {
                        departmentName = "Admin"
                    });
                }

                if (!db.Status.Any())
                {
                    db.Status.Add(new Status
                    {
                        statusName = "Opened"
                    });

                    db.Status.Add(new Status
                    {
                        statusName = "In Progress"
                    });

                    db.Status.Add(new Status
                    {
                        statusName = "Waiting for closing"
                    });

                    db.Status.Add(new Status
                    {
                        statusName = "Closed"
                    });
                }

                db.SaveChanges();

                if (!db.Users.Any())
                {
                    User user = new User
                    {
                        userFname = "Admin",
                        userLname = "Admin",
                        username = "******",
                        password = "******",
                        userTel = "1111",
                        userEmail = "*****@*****.**",
                        Department = db.Departments.First(),
                        userCode = "1111",
                        userPosition = "Admin"
                    };
                    user.Roles.Add(db.Roles.Single(x=>x.Name == "Admin"));

                    db.Users.Add(user);
                }

                db.SaveChanges();
            }
        }