Пример #1
0
        public ActionResult Create(vEPSROLE_Manage VRM)
        {
            //初始化系統參數
            Configer.Init();

            //Log記錄用
            SYSTEMLOG SL = new SYSTEMLOG();
            SL.UId = Session["UserID"].ToString();
            SL.Controller = "Role";
            SL.Action = "Create";
            SL.TotalCount = 1;
            SL.StartDateTime = DateTime.Now;

            string MailServer = Configer.MailServer;
            int MailServerPort = Configer.MailServerPort;
            string MailSender = Configer.MailSender;
            List<string> MailReceiver = Configer.MailReceiver;

            try
            {
                if (ModelState.IsValid)
                {
                    EPSROLE R = new EPSROLE();
                    R.RoleName = VRM.RoleName;
                    R.CreateAccount = Session["UserID"].ToString().Trim();
                    R.CreateTime = DateTime.Now;
                    R.UpadteAccount = Session["UserID"].ToString().Trim();
                    R.UpdateTime = DateTime.Now;

                    context.EPSROLES.Add(R);
                    context.SaveChanges();

                    foreach (var item in VRM.FuncList)
                    {
                        ROLEFUNCMAPPING RM = new ROLEFUNCMAPPING();
                        RM.RId = R.RId;
                        RM.FId = item;
                        RM.CreateAccount = Session["UserID"].ToString().Trim();
                        RM.CreateTime = DateTime.Now;
                        RM.UpadteAccount = Session["UserID"].ToString().Trim();
                        RM.UpdateTime = DateTime.Now;

                        context.ROLEFUNCMAPPINGS.Add(RM);
                        context.SaveChanges();
                    }
                    SL.EndDateTime = DateTime.Now;
                    SL.SuccessCount = 1;
                    SL.FailCount = 0;
                    SL.Result = true;
                    SL.Msg = "建立使用者作業成功";
                    SF.log2DB(SL, MailServer, MailServerPort, MailSender, MailReceiver);

                    //TempData["CreateMsg"] = "<script>alert('新增成功');</script>";

                    return RedirectToAction("Index", "Role");
                }
                else
                {
                    TempData["CreateMsg"] = "<script>alert('新增失敗');</script>";

                    return RedirectToAction("Create", "Role");
                }
            }
            catch (Exception ex)
            {
                SL.EndDateTime = DateTime.Now;
                SL.TotalCount = 1;
                SL.SuccessCount = 0;
                SL.FailCount = 1;
                SL.Result = false;
                SL.Msg = "建立角色作業失敗," + "錯誤訊息[" + ex.ToString() + "]";
                SF.log2DB(SL, MailServer, MailServerPort, MailSender, MailReceiver);

                TempData["CreateMsg"] = "<script>alert('發生異常');</script>";

                return RedirectToAction("Create", "Role");
            }
        }
Пример #2
0
        public ActionResult Edit(vEPSROLE_Manage VRM)
        {
            //初始化系統參數
            Configer.Init();

            //Log記錄用
            SYSTEMLOG SL = new SYSTEMLOG();
            SL.UId = Session["UserID"].ToString();
            SL.Controller = "Role";
            SL.Action = "Edit";
            SL.TotalCount = 1;
            SL.StartDateTime = DateTime.Now;

            string MailServer = Configer.MailServer;
            int MailServerPort = Configer.MailServerPort;
            string MailSender = Configer.MailSender;
            List<string> MailReceiver = Configer.MailReceiver;

            try
            {
                EPSROLE R = context.EPSROLES.Find(VRM.RId);
                R.RoleName = VRM.RoleName;
                R.UpadteAccount= Session["UserID"].ToString().Trim();
                R.UpdateTime = DateTime.Now;
                //取得目前角色擁有的權限
                var query1 = from rm in context.ROLEFUNCMAPPINGS
                             where rm.RId ==VRM.RId
                             select new
                             {
                                 rm.FId
                             };
                List<int> TmpList = new List<int>();
                foreach (var item in query1)
                {
                    TmpList.Add(item.FId);
                }

                //取得新增清單
                var addResultList = VRM.FuncList.Except(TmpList);
                //取得移除清單
                var delResultList = TmpList.Except(VRM.FuncList);

                //新增權限作業
                if (addResultList !=null)
                {
                    foreach (var item in addResultList)
                    {
                        ROLEFUNCMAPPING RM = new ROLEFUNCMAPPING();
                        RM.RId = VRM.RId;
                        RM.FId = item;
                        RM.CreateAccount = Session["UserID"].ToString().Trim();
                        RM.CreateTime = DateTime.Now;
                        RM.UpadteAccount = Session["UserID"].ToString().Trim();
                        RM.UpdateTime = DateTime.Now;

                        context.ROLEFUNCMAPPINGS.Add(RM);
                        context.SaveChanges();
                    }
                }

                //移除權限作業
                if (delResultList !=null)
                {
                    foreach (var item in delResultList)
                    {
                        ROLEFUNCMAPPING RM = context.ROLEFUNCMAPPINGS.Where(b => b.RId == VRM.RId)
                                                                     .Where(b => b.FId == item)
                                                                     .First();
                        context.Entry(RM).State = EntityState.Deleted;
                        context.SaveChanges();
                    }
                }

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

                SL.EndDateTime = DateTime.Now;
                SL.SuccessCount = 1;
                SL.FailCount = 0;
                SL.Result = true;
                SL.Msg = "編輯角色作業成功,RId:[" + VRM.RId.ToString() + "]";
                SF.log2DB(SL, MailServer, MailServerPort, MailSender, MailReceiver);

                return RedirectToAction("Index", "Role");
            }
            catch (Exception ex)
            {
                SL.EndDateTime = DateTime.Now;
                SL.TotalCount = 1;
                SL.SuccessCount = 0;
                SL.FailCount = 1;
                SL.Result = false;
                SL.Msg = "編輯角色作業失敗," + "錯誤訊息[" + ex.ToString() + "]";
                SF.log2DB(SL, MailServer, MailServerPort, MailSender, MailReceiver);

                TempData["EditMsg"] = "<script>alert('發生異常');</script>";

                return RedirectToAction("Edit", "Role",new { RId= VRM.RId });
            }
        }