Esempio n. 1
0
        public ActionResult MenuSystem(int siteId, string name = "_lyMenu")
        {
            UnitOfWork     uw = new UnitOfWork();
            List <SysMenu> md = new List <SysMenu>();

            if (Session["sid"] != null)
            {
                long           uid    = long.TryParse(Session["sid"]?.ToString(), out uid) ? uid : 0;
                SysAccount     usr    = uw.Account.Get(uid);
                SysAccountRole role   = usr.Role(siteId);
                int            roleId = role.RoleId;
                var            data   = (from d in db.SysMenu
                                         join r in db.SysRolePermission on d.MenuId equals r.MenuId
                                         where
                                         d.MenuLevel == 1 &&
                                         d.SiteId == siteId &&
                                         d.FlagActive &&
                                         r.RoleId == roleId &&
                                         r.PermissionId == 1 &&
                                         r.PermissionFlag
                                         orderby d.MenuOrder ascending
                                         select d);
                md = data.ToList();
                ViewData["RoleId"] = roleId;
            }
            //if (ViewData["MenuCurrent"] != null) { md.Current = (int)ViewData["MenuCurrent"]; }
            //md.Data = data.ToList();
            return(PartialView(name, md));
        }
Esempio n. 2
0
        public void SetRole(int siteId, int roleId, SysAccount ob)
        {
            SysAccountRole role = ob.AccountId > 0 ?
                                  ((from d in db.SysAccountRole
                                    join r in db.SysRole on d.RoleId equals r.RoleId
                                    where
                                    d.AccountId == ob.AccountId &&
                                    r.SiteId == siteId
                                    select d).FirstOrDefault() ?? new SysAccountRole()
            {
                Account = ob
            }) :
                                  new SysAccountRole()
            {
                Account = ob
            };

            role.RoleId = roleId;
            if (role.Id <= 0)
            {
                db.SysAccountRole.Add(role);
            }
            else
            {
                db.SysAccountRole.Update(role);
            }
        }
Esempio n. 3
0
        public ActionResult SetDetail()
        {
            int        accountId = Request.Form["account_id"].ParseInt();
            SysAccount ob        = uow.Account.Get(accountId);

            if (ob.AccountId <= 0)
            {
                ob.AccountPassword = Request.Form["account_password"];
                ob.CreatedBy       = CurrentUID;
                ob.CreatedDate     = CurrentDateTime;
            }
            ob.AccountFirstName = Request.Form["account_firstname"];
            ob.AccountLastName  = Request.Form["account_lastname"];
            ob.AccountUsername  = "******" + CurrentDateTime.ToString("yyyyMMddHHmmss"); //Request.Form["account_username"];
            ob.AccountEmail     = Request.Form["account_email"];
            ob.FlagStatus       = Request.Form["flag_status"].ParseInt();
            ob.FlagAdminCalc    = Request.Form["flag_admin_cal"].ParseBoolean();
            ob.UpdatedBy        = CurrentUID;
            ob.UpdatedDate      = CurrentDateTime;
            try
            {
                if (!ob.ValidateModel(out string errMsg))
                {
                    throw new Exception(errMsg);
                }

                if (Request.Files.Count > 0 && Request.Files["AccountAvatar"] != null && Request.Files["AccountAvatar"].ContentLength > 0)
                {
                    HttpPostedFileBase uploadedFile = Request.Files["AccountAvatar"];
                    string             FilePath     = string.Format("files/avatar/{0}{1}", CurrentDate.ParseString(DateFormat._yyyyMMddHHmmssfff), Path.GetExtension(uploadedFile.FileName));
                    if (!Directory.Exists(Server.MapPath("~/files")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/files"));
                    }
                    if (!Directory.Exists(Server.MapPath("~/files/avatar")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/files/avatar"));
                    }
                    uploadedFile.SaveAs(Server.MapPath("~/" + FilePath));

                    ob.AccountAvatar = FilePath;
                }

                if ((ob.AccountId <= 0 && ob.AccountPassword != Request.Form["account_passwordre"]) ||
                    (ob.AccountId > 0 && !string.IsNullOrWhiteSpace(Request.Form["account_password"]) && Request.Form["account_password"] != Request.Form["account_passwordre"]))
                {
                    throw new Exception("รหัสผ่านไม่ตรงกัน");
                }

                if (!ob.FlagSystem)
                {
                    SysAccountRole roleMapped = ob.AccountId > 0 ?
                                                ((from d in uow.db.SysAccountRole
                                                  join r in uow.db.SysRole on d.RoleId equals r.RoleId
                                                  where
                                                  d.AccountId == ob.AccountId &&
                                                  r.SiteId == SITE_ID
                                                  select d).FirstOrDefault() ?? new SysAccountRole()
                    {
                        SysAccount = ob
                    }) :
                                                new SysAccountRole()
                    {
                        SysAccount = ob
                    };
                    roleMapped.RoleId = Request.Form["role_id"].ParseInt();
                    if (roleMapped.Id <= 0)
                    {
                        uow.db.SysAccountRole.Add(roleMapped);
                    }
                    else
                    {
                        uow.db.Entry(roleMapped).State = System.Data.Entity.EntityState.Modified;
                    }
                }

                if (ob.AccountId <= 0)
                {
                    ob.AccountPassword = Crypto.HashPassword(ob.AccountPassword);
                }
                else if (ob.AccountId > 0 && !string.IsNullOrWhiteSpace(Request.Form["account_password"]))
                {
                    ob.AccountPassword = Crypto.HashPassword(Request.Form["account_password"]);
                }

                uow.Account.Set(ob);
                if (ob.FlagAdminCalc && ob.AccountId > 0)
                {
                    var delData = db.CalcAccountStaff.Where(x => x.AccountId == ob.AccountId);
                    db.CalcAccountStaff.RemoveRange(delData);
                    db.SaveChanges();
                }
                uow.SaveChanges();

                return(RedirectToAction("Index", new {
                    area = "",
                    controller = "Account",
                    msg = "บันทึกข้อมูลเรียบร้อยแล้ว",
                    msgType = AlertMsgType.Success
                }));
            }
            catch (DbEntityValidationException ex)
            {
                string msg = "";
                foreach (var eve in ex.EntityValidationErrors)
                {
                    msg += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                         eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        msg += string.Format("{{\n}}- Property: \"{0}\", Error: \"{1}\"",
                                             ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return(ViewDetail(ob, msg, AlertMsgType.Danger));
            }
            catch (Exception ex)
            {
                string msg = ex.GetMessage();
                return(ViewDetail(ob, msg, AlertMsgType.Danger));
            }
        }