public void addModule(int module, int ID, int moduleID)
 {
     if (module == 1)
     {
         var listRight   = db.Account_Right.Where(x => x.ModuleID == moduleID + "").ToList();
         var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("Account.GetFunctionsByModuleAndUID {0}, {1}", ID, moduleID).ToList();
         foreach (var r in rightRemove)
         {
             var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
             db.Account_Right_Detail.Remove(del);
         }
         db.SaveChanges();
         foreach (var r in listRight)
         {
             if (!String.IsNullOrEmpty(r.ID + ""))
             {
                 Account_Right_Detail rd = new Account_Right_Detail()
                 {
                     AccountID = ID,
                     RightID   = r.ID
                 };
                 db.Account_Right_Detail.Add(rd);
             }
         }
         db.SaveChanges();
     }
 }
 public void updateModule(int module, int ID, bool pb, int moduleID)
 {
     if (Convert.ToBoolean(module).Equals(pb))
     {
     }
     else
     {
         var listRight     = db.Account_Right.Where(x => x.ModuleID == moduleID + "").ToList();
         var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("Account.GetFunctionsByModuleAndUID {0}, {1}", ID, moduleID).ToList();
         foreach (var r in rightRemoveup)
         {
             var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
             db.Account_Right_Detail.Remove(del);
         }
         if (module == 0)
         {
         }
         else
         {
             db.SaveChanges();
             foreach (var r in listRight)
             {
                 if (!String.IsNullOrEmpty(r.ID + ""))
                 {
                     Account_Right_Detail rd = new Account_Right_Detail()
                     {
                         AccountID = ID,
                         RightID   = r.ID
                     };
                     db.Account_Right_Detail.Add(rd);
                 }
             }
             db.SaveChanges();
         }
     }
 }
        public JsonResult UpdateUser(int ID, string Name, string Username, string Position, string Password, string RepeatPassword, string NVID,
                                     int module1, int module2, int module3, int module4, int module5, int module6, int module7,
                                     int module8, int module9, int module11, int module12, int module13, int module14,
                                     int module15, int module17, int module18, int module19, int module20, string rights)
        {
            if (db.Accounts.Where(x => x.Username == Username).Where(y => y.ID != ID).Count() > 0)
            {
                return(Json(new Result()
                {
                    CodeError = 2,
                    Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!"
                }, JsonRequestBehavior.AllowGet));
            }
            if (!String.IsNullOrEmpty(NVID))
            {
                var nv = db.Employees.Where(x => x.employee_id.Equals(NVID)).FirstOrDefault();
                if (nv == null)
                {
                    return(Json(new Result()
                    {
                        CodeError = 2,
                        Data = "Mã nhân viên <strong style='color:black; '>" + NVID + "</strong> không tồn tại!"
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                NVID = null;
            }
            string InvalidFields = "";

            if (String.IsNullOrEmpty(Name))
            {
                InvalidFields += "Họ Tên -";
            }
            if (String.IsNullOrEmpty(Username))
            {
                InvalidFields += " Tên đăng nhập -";
            }
            if (String.IsNullOrEmpty(Position))
            {
                InvalidFields += " Chức vụ -";
            }
            if (InvalidFields != "")
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = InvalidFields.Substring(0, InvalidFields.Length) + " không thể để trống!"
                }, JsonRequestBehavior.AllowGet));
            }
            if (Password != RepeatPassword)
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = "Mật khẩu không khớp!"
                }, JsonRequestBehavior.AllowGet));
            }
            var user = db.Accounts.SingleOrDefault(x => x.ID == ID);

            using (DbContextTransaction trans = db.Database.BeginTransaction())
            {
                try
                {
                    var rightsSplit = rights.Split(',');
                    var rightRemove = db.Account_Right_Detail.Where(x => x.AccountID == ID).ToList();
                    foreach (var r in rightRemove)
                    {
                        db.Account_Right_Detail.Remove(r);
                    }
                    db.SaveChanges();
                    foreach (var r in rightsSplit)
                    {
                        if (!String.IsNullOrEmpty(r))
                        {
                            Account_Right_Detail rd = new Account_Right_Detail()
                            {
                                AccountID = ID,
                                RightID   = int.Parse(r)
                            };
                            db.Account_Right_Detail.Add(rd);
                        }
                    }
                    db.SaveChanges();
                    updateModule(module1, ID, user.CDVT, 1);
                    updateModule(module2, ID, user.TCLD, 2);
                    updateModule(module3, ID, user.KCS, 3);
                    updateModule(module4, ID, user.DK, 4);
                    updateModule(module5, ID, user.BGD, 5);
                    updateModule(module6, ID, user.PXKT, 6);
                    updateModule(module8, ID, user.PXDL, 8);
                    updateModule(module9, ID, user.PXVT, 9);
                    updateModule(module11, ID, user.PXPV, 11);
                    updateModule(module12, ID, user.PXDS, 12);
                    updateModule(module13, ID, user.PXCDM, 13);
                    updateModule(module14, ID, user.PXTGQLM, 14);
                    updateModule(module15, ID, user.PXXD, 15);
                    updateModule(module17, ID, user.AT, 17);
                    updateModule(module18, ID, user.KCM, 18);
                    updateModule(module19, ID, Convert.ToBoolean(user.PXCKSC), 19);
                    updateModule(module20, ID, user.PXCBT, 20);
                    if (Convert.ToBoolean(module7).Equals(user.ADMIN))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("Account.GetFunctionsByUID {0}", ID).ToList();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module7 == 0)
                        {
                            module1   = 0; module2 = 0; module3 = 0; module4 = 0; module5 = 0; module6 = 0; module7 = 0;
                            module8   = 0; module9 = 0; module11 = 0; module12 = 0; module13 = 0; module14 = 0;
                            module15  = 0; module17 = 0; module18 = 0; module19 = 0; module20 = 0;
                            user.Role = 3;
                        }
                        else
                        {
                            module7   = 1;
                            user.Role = 2;
                            db.SaveChanges();
                        }
                    }
                    user.Name     = Name;
                    user.Username = Username;
                    if (String.IsNullOrEmpty(Password))
                    {
                    }
                    else
                    {
                        string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl");
                        user.Password = passXc;
                    }
                    user.Position = Position;
                    user.NVID     = NVID;

                    user.CDVT            = Convert.ToBoolean(module1);
                    user.TCLD            = Convert.ToBoolean(module2);
                    user.KCS             = Convert.ToBoolean(module3);
                    user.DK              = Convert.ToBoolean(module4);
                    user.BGD             = Convert.ToBoolean(module5);
                    user.PXKT            = Convert.ToBoolean(module6);
                    user.ADMIN           = Convert.ToBoolean(module7);
                    user.PXDL            = Convert.ToBoolean(module8);
                    user.PXVT            = Convert.ToBoolean(module9);
                    user.PXPV            = Convert.ToBoolean(module11);
                    user.PXDS            = Convert.ToBoolean(module12);
                    user.PXCDM           = Convert.ToBoolean(module13);
                    user.PXTGQLM         = Convert.ToBoolean(module14);
                    user.PXXD            = Convert.ToBoolean(module15);
                    user.AT              = Convert.ToBoolean(module17);
                    user.PXCKSC          = Convert.ToBoolean(module19);
                    user.KCM             = Convert.ToBoolean(module18);
                    user.PXCBT           = Convert.ToBoolean(module20);
                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();
                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    return(Json(new Result()
                    {
                        CodeError = 2,
                        Data = "Có lỗi vui lòng kiểm tra lại!"
                    }, JsonRequestBehavior.AllowGet));
                }
            }

            return(Json(new Result()
            {
                CodeError = 0,
                Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được cập nhật thành công cho <strong style='color:black;'>" + Name + "</strong>"
            }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult AddNewUser(string Name, string Username, string Position, string Password, string RepeatPassword, string NVID,
                                     int module1, int module2, int module3, int module4, int module5, int module6, int module7,
                                     int module8, int module9, int module11, int module12, int module13, int module14,
                                     int module15, int module17, int module18, int module19, int module20, string rights)
        {
            if (db.Accounts.Where(x => x.Username == Username).Count() > 0)
            {
                return(Json(new Result()
                {
                    CodeError = 2,
                    Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!"
                }, JsonRequestBehavior.AllowGet));
            }
            string InvalidFields = "";

            if (!String.IsNullOrEmpty(NVID))
            {
                if (db.Employees.Where(x => x.employee_id == NVID).Count() == 0)
                {
                    return(Json(new Result()
                    {
                        CodeError = 2,
                        Data = "Mã nhân viên <strong style='color:black; '>" + NVID + "</strong> không tồn tại!"
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                InvalidFields += " Mã nhân viên -";
            }
            if (String.IsNullOrEmpty(Name))
            {
                InvalidFields += " Họ Tên -";
            }
            if (String.IsNullOrEmpty(Username))
            {
                InvalidFields += " Tên đăng nhập -";
            }
            if (String.IsNullOrEmpty(Position))
            {
                InvalidFields += " Chức vụ -";
            }
            if (InvalidFields != "")
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = InvalidFields.Substring(0, InvalidFields.Length) + " không được để trống !!!"
                }, JsonRequestBehavior.AllowGet));
            }
            if (String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(RepeatPassword))
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = "Mật khẩu không được để trống !!!"
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                if (Password != RepeatPassword)
                {
                    return(Json(new Result()
                    {
                        CodeError = 1,
                        Data = "Mật khẩu nhập lại không khớp !!!"
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            if (InvalidFields != "")
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = InvalidFields
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl");
                using (DbContextTransaction trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        Account a = new Account()
                        {
                            Name     = Name,
                            Username = Username,
                            Password = passXc,
                            Position = Position,
                            NVID     = NVID,
                            Role     = 3,
                            CDVT     = Convert.ToBoolean(module1),
                            TCLD     = Convert.ToBoolean(module2),
                            KCS      = Convert.ToBoolean(module3),
                            DK       = Convert.ToBoolean(module4),
                            BGD      = Convert.ToBoolean(module5),
                            PXKT     = Convert.ToBoolean(module6),
                            PXDL     = Convert.ToBoolean(module8),
                            PXVT     = Convert.ToBoolean(module9),
                            PXPV     = Convert.ToBoolean(module11),
                            PXDS     = Convert.ToBoolean(module12),
                            PXCDM    = Convert.ToBoolean(module13),
                            PXTGQLM  = Convert.ToBoolean(module14),
                            PXXD     = Convert.ToBoolean(module15),
                            AT       = Convert.ToBoolean(module17),
                            PXCKSC   = Convert.ToBoolean(module19),
                            KCM      = Convert.ToBoolean(module18),
                            PXCBT    = Convert.ToBoolean(module20)
                        };
                        db.Accounts.Add(a);
                        db.SaveChanges();
                        var acc         = db.Accounts.Where(x => x.Username == Username).FirstOrDefault();
                        var rightsSplit = rights.Split(',');
                        foreach (var r in rightsSplit)
                        {
                            if (!String.IsNullOrEmpty(r))
                            {
                                Account_Right_Detail rd = new Account_Right_Detail()
                                {
                                    AccountID = acc.ID,
                                    RightID   = int.Parse(r)
                                };
                                db.Account_Right_Detail.Add(rd);
                            }
                        }
                        db.SaveChanges();
                        addModule(module1, acc.ID, 1);
                        addModule(module2, acc.ID, 2);
                        addModule(module3, acc.ID, 3);
                        addModule(module4, acc.ID, 4);
                        addModule(module5, acc.ID, 5);
                        addModule(module6, acc.ID, 6);
                        addModule(module8, acc.ID, 8);
                        addModule(module9, acc.ID, 9);
                        addModule(module11, acc.ID, 11);
                        addModule(module12, acc.ID, 12);
                        addModule(module13, acc.ID, 13);
                        addModule(module14, acc.ID, 14);
                        addModule(module15, acc.ID, 15);
                        addModule(module17, acc.ID, 17);
                        addModule(module18, acc.ID, 18);
                        addModule(module19, acc.ID, 19);
                        addModule(module20, acc.ID, 20);
                        if (module7 == 1)
                        {
                            var user = db.Accounts.SingleOrDefault(x => x.ID == acc.ID);
                            user.Name            = Name;
                            user.Username        = Username;
                            user.Password        = passXc;
                            user.Position        = Position;
                            user.Role            = 2;
                            user.NVID            = NVID;
                            user.CDVT            = false;
                            user.TCLD            = false;
                            user.KCS             = false;
                            user.DK              = false;
                            user.BGD             = false;
                            user.PXKT            = false;
                            user.ADMIN           = true;
                            user.PXDL            = false;
                            user.PXVT            = false;
                            user.PXPV            = false;
                            user.PXDS            = false;
                            user.PXCDM           = false;
                            user.PXTGQLM         = false;
                            user.PXXD            = false;
                            user.AT              = false;
                            user.KCM             = false;
                            user.PXCKSC          = false;
                            user.PXCBT           = false;
                            db.Entry(user).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        trans.Commit();
                        return(Json(new Result()
                        {
                            CodeError = 0,
                            Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được thêm mới thành công cho <strong style='color:black;'>" + Name + "</strong>"
                        }, JsonRequestBehavior.AllowGet));
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        return(Json(new Result()
                        {
                            CodeError = 2,
                            Data = "Có lỗi vui lòng kiểm tra lại!"
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public JsonResult UpdateUser(int ID, string Name, string Username, string Position, string Password, string RepeatPassword,
                                     int module1, int module2, int module3, int module4, int module5, int module6, int module7, string rights)
        {
            if (db.Accounts.Where(x => x.Username == Username).Where(y => y.ID != ID).Count() > 0)
            {
                return(Json(new Result()
                {
                    CodeError = 2,
                    Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!"
                }, JsonRequestBehavior.AllowGet));
            }
            string InvalidFields = "";

            if (String.IsNullOrEmpty(Name))
            {
                InvalidFields += "Họ Tên-";
            }
            if (String.IsNullOrEmpty(Username))
            {
                InvalidFields += "Tên đăng nhập-";
            }
            if (String.IsNullOrEmpty(Position))
            {
                InvalidFields += "Chức vụ-";
            }
            if (Password != RepeatPassword)
            {
                InvalidFields += "<br />Mật khảu không khớp !!!";
            }
            if (InvalidFields != "")
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = InvalidFields
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var user = db.Accounts.SingleOrDefault(x => x.ID == ID);
                try
                {
                    var rightsSplit = rights.Split(',');
                    var rightRemove = db.Account_Right_Detail.Where(x => x.AccountID == ID).ToList();
                    foreach (var r in rightRemove)
                    {
                        db.Account_Right_Detail.Remove(r);
                    }
                    db.SaveChanges();
                    foreach (var r in rightsSplit)
                    {
                        if (!String.IsNullOrEmpty(r))
                        {
                            Account_Right_Detail rd = new Account_Right_Detail()
                            {
                                AccountID = ID,
                                RightID   = int.Parse(r)
                            };
                            db.Account_Right_Detail.Add(rd);
                        }
                    }
                    db.SaveChanges();
                    if (Convert.ToBoolean(module1).Equals(user.CDVT))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 1 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='1'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module1 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module2).Equals(user.TCLD))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 2 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='2'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module2 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module3).Equals(user.KCS))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 3 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='3'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module3 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module4).Equals(user.DK))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 4 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='4'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module4 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module5).Equals(user.BGD))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 5 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='5'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module5 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module6).Equals(user.PXKT))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.Where(x => x.ModuleID == 6 + "").ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='6'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module6 == 0)
                        {
                        }
                        else
                        {
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                    }
                    if (Convert.ToBoolean(module7).Equals(user.ADMIN))
                    {
                    }
                    else
                    {
                        var listRight     = db.Account_Right.ToList();
                        var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "'").ToList <Account_Right_Detail>();
                        foreach (var r in rightRemoveup)
                        {
                            var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault();
                            db.Account_Right_Detail.Remove(del);
                        }
                        if (module7 == 0)
                        {
                            module1 = 0; module2 = 0; module3 = 0; module4 = 0; module5 = 0; module6 = 0; module7 = 0;
                        }
                        else
                        {
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                            module1 = 1;
                            module2 = 1;
                            module3 = 1;
                            module4 = 1;
                            module5 = 1;
                            module6 = 1;
                            module7 = 1;
                            db.SaveChanges();
                        }
                    }
                    user.Name     = Name;
                    user.Username = Username;
                    if (String.IsNullOrEmpty(Password))
                    {
                    }
                    else
                    {
                        string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl");
                        user.Password = passXc;
                    }
                    user.Position        = Position;
                    user.CDVT            = Convert.ToBoolean(module1);
                    user.TCLD            = Convert.ToBoolean(module2);
                    user.KCS             = Convert.ToBoolean(module3);
                    user.DK              = Convert.ToBoolean(module4);
                    user.BGD             = Convert.ToBoolean(module5);
                    user.PXKT            = Convert.ToBoolean(module6);
                    user.ADMIN           = Convert.ToBoolean(module7);
                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    return(Json(new Result()
                    {
                        CodeError = 2,
                        Data = "Có lỗi vui lòng kiểm tra lại!"
                    }, JsonRequestBehavior.AllowGet));
                }
                return(Json(new Result()
                {
                    CodeError = 0,
                    Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được cập nhật thành công cho <strong style='color:black;'>" + Name + "</strong>"
                }, JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 6
0
        public JsonResult AddNewUser(string Name, string Username, string Position, string Password, string RepeatPassword,
                                     int module1, int module2, int module3, int module4, int module5, int module6, int module7, string rights)
        {
            if (db.Accounts.Where(x => x.Username == Username).Count() > 0)
            {
                return(Json(new Result()
                {
                    CodeError = 2,
                    Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!"
                }, JsonRequestBehavior.AllowGet));
            }
            string InvalidFields = "";

            if (String.IsNullOrEmpty(Name))
            {
                InvalidFields += "Họ Tên -";
            }
            if (String.IsNullOrEmpty(Username))
            {
                InvalidFields += "Tên đăng nhập -";
            }
            if (String.IsNullOrEmpty(Position))
            {
                InvalidFields += "Chức vụ -";
            }
            if (InvalidFields != "")
            {
                InvalidFields += " không thể để trống !!!";
            }
            if (String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(RepeatPassword))
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = "Mật khẩu không được để trống !!!"
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                if (Password != RepeatPassword)
                {
                    InvalidFields += "<br />Mật khảu không khớp !!!";
                }
            }
            if (InvalidFields != "")
            {
                return(Json(new Result()
                {
                    CodeError = 1,
                    Data = InvalidFields
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                //var listRightBasic = db.Database.SqlQuery<rightBasic>("select a.ID from Account_Right a where a.isBasic = '1' and a.ModuleID='1'").ToList<rightBasic>();
                string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl");
                using (DbContextTransaction trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        Account a = new Account()
                        {
                            Name     = Name,
                            Username = Username,
                            Password = passXc,
                            Position = Position,
                            CDVT     = Convert.ToBoolean(module1),
                            TCLD     = Convert.ToBoolean(module2),
                            KCS      = Convert.ToBoolean(module3),
                            DK       = Convert.ToBoolean(module4),
                            BGD      = Convert.ToBoolean(module5),
                            PXKT     = Convert.ToBoolean(module6)
                        };
                        db.Accounts.Add(a);
                        db.SaveChanges();
                        var acc         = db.Accounts.Where(x => x.Username == Username).FirstOrDefault();
                        var rightsSplit = rights.Split(',');
                        foreach (var r in rightsSplit)
                        {
                            if (!String.IsNullOrEmpty(r))
                            {
                                Account_Right_Detail rd = new Account_Right_Detail()
                                {
                                    AccountID = acc.ID,
                                    RightID   = int.Parse(r)
                                };
                                db.Account_Right_Detail.Add(rd);
                            }
                        }
                        db.SaveChanges();
                        if (module1 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 1 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='1'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            db.SaveChanges();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module2 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 2 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='2'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module3 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 3 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='3'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module4 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 4 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='4'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module5 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 5 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='5'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module6 == 1)
                        {
                            var listRight   = db.Account_Right.Where(x => x.ModuleID == 6 + "").ToList();
                            var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='6'").ToList <Account_Right_Detail>();
                            foreach (var r in rightRemove)
                            {
                                var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault();
                                db.Account_Right_Detail.Remove(del);
                            }
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            db.SaveChanges();
                        }
                        if (module7 == 1)
                        {
                            var listRight = db.Account_Right.ToList();
                            foreach (var r in listRight)
                            {
                                if (!String.IsNullOrEmpty(r.ID + ""))
                                {
                                    Account_Right_Detail rd = new Account_Right_Detail()
                                    {
                                        AccountID = acc.ID,
                                        RightID   = r.ID
                                    };
                                    db.Account_Right_Detail.Add(rd);
                                }
                            }
                            var user = db.Accounts.SingleOrDefault(x => x.ID == acc.ID);
                            user.Name            = Name;
                            user.Username        = Username;
                            user.Password        = passXc;
                            user.Position        = Position;
                            user.CDVT            = true;
                            user.TCLD            = true;
                            user.KCS             = true;
                            user.DK              = true;
                            user.BGD             = true;
                            user.PXKT            = true;
                            user.ADMIN           = true;
                            db.Entry(user).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        trans.Commit();
                        return(Json(new Result()
                        {
                            CodeError = 0,
                            Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được thêm mới thành công cho <strong style='color:black;'>" + Name + "</strong>"
                        }, JsonRequestBehavior.AllowGet));
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                        return(Json(new Result()
                        {
                            CodeError = 2,
                            Data = "Có lỗi vui lòng kiểm tra lại!"
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
            }
        }