Example #1
0
        private void New()
        {
            var per = new TB_Permission();

            per.Name      = txtName.Value.Trim();
            per.Image     = hidImage.Value;
            per.Delete    = false;
            per.IsDefault = isDefault.Checked;
            per.Url       = txtURL.Value.Trim();
            // 父级菜单,为空时默认为顶级菜单
            var parent = int.Parse("" == hidParent.Value ? "0" : hidParent.Value);

            per.Parent = parent;
            var brothers = PermissionInstance.FindList(p => p.Parent == parent);

            per.DisplayOrder = brothers.Count();
            per.AddTime      = DateTime.Now;
            per.Description  = txtDescription.Value.Trim();
            PermissionInstance.Add(per);

            // 记录历史
            SaveHistory(new TB_AccountHistory
            {
                ActionId = ActionInstance.Find(f => f.Name.Equals("AddPermission")).id,
                ObjectA  = "[id=" + per.id + "] " + per.Name
            });
            UpdateRole(per);
            ShowNotification("./permission_list.aspx", "Success: You added a new menu.", true);
        }
Example #2
0
        /// <summary>
        /// 根据编辑的菜单项更新角色的访问
        /// </summary>
        /// <param name="obj"></param>
        private void UpdateRole(TB_Permission obj)
        {
            // 更新默认角色可以访问的
            if (obj.IsDefault == true)
            {
                var dftPermission = PermissionInstance.GetDefaultMenus();
                // 查找非管理角色
                var roles = RoleInstance.FindList(f => f.IsAdministrator == false && f.Delete == false);
                foreach (var role in roles)
                {
                    var pers = role.Permission.Split(new char[] { ',' });
                    if (!pers.Contains(obj.id.ToString()))
                    {
                        RoleInstance.Update(f => f.id == role.id, act => act.Permission = dftPermission);
                    }
                }
            }
            // 查找更新管理员角色的访问权限
            RoleInstance.Update(f => f.IsAdministrator == true && f.Delete == false,
                                act => act.Permission = PermissionInstance.GetAdministratorsMenus());

            // 重置当前登陆者的session
            Account = AccountInstance.Find(f => f.id == Account.id);
            Session[Utility.SessionName] = Account;
        }
Example #3
0
        private void init()
        {
            var id = ParseInt(Utility.Decrypt(_key));

            if (id <= 0)
            {
                ShowNotification("./permission_list.aspx", "Error: paramenter error, cannot edit the permission.", false);
            }
            else
            {
                var per = PermissionInstance.Find(p => p.id == id);
                if (null == per)
                {
                    ShowNotification("./permission_list.aspx", "Error: Cannot edit permission, object not exist.", false);
                }
                else
                {
                    txtDescription.Value = per.Description;
                    txtName.Value        = per.Name;
                    hidParent.Value      = per.Parent.ToString();
                    if (per.Parent > 0)
                    {
                        var parent = PermissionInstance.Find(f => f.id == per.Parent);
                        txtParent.Value = parent.Name;
                    }
                    isDefault.Checked = per.IsDefault.Value;
                    txtURL.Value      = per.Url;
                    imgImage.Src      = per.Image;
                    hidImage.Value    = per.Image;
                }
            }
        }
Example #4
0
 protected void btSave_Click(object sender, EventArgs e)
 {
     if (hidID.Value != "")
     {
         var per = PermissionInstance.Find(f => f.id == int.Parse(Utility.Decrypt(hidID.Value)));
         if (null == per)
         {
             // 无法进行更新
             ShowNotification("./permission_list.aspx", "Cannot edit permission: object not exist.", false);
             return;
         }
         else
         {
             per.Name        = txtName.Value.Trim();
             per.Image       = hidImage.Value;
             per.Description = txtDescription.Value.Trim();
             per.IsDefault   = isDefault.Checked;
             per.Url         = txtURL.Value.Trim();
             Edit(int.Parse(hidParent.Value), per);
             UpdateRole(per);
         }
     }
     else
     {
         New();
     }
 }
Example #5
0
        private void ShowPermissions()
        {
            List <int> menus;

            if ("" == hidParent.Value)
            {
                menus = PermissionInstance.GetAllMenus();
            }
            else
            {
                menus = PermissionInstance.GetSubmenus(ParseInt(hidParent.Value));
            }

            var totalRecords = 0;
            var pageIndex    = "" == hidPageIndex.Value ? 1 : int.Parse(hidPageIndex.Value);
            var list         = PermissionInstance.FindPageList <TB_Permission>(pageIndex, PageSize, out totalRecords,
                                                                               p => p.Delete == false && (p.Name.IndexOf(txtName.Value.Trim()) >= 0) && menus.Contains(p.id), "Parent,DisplayOrder");
            var totalPages = totalRecords / PageSize + (totalRecords % PageSize > 0 ? 1 : 0);

            list.OrderBy(o => o.id).ThenBy(t => t.Name);
            string html = "";

            if (totalRecords < 1)
            {
                html = "<tr><td colspan=\"8\">No records, You can change the condition and try again or " +
                       " <a href=\"./permission_add.aspx\">ADD</a> new one.</td></tr>";
            }
            else
            {
                var cnt = (pageIndex - 1) * PageSize;
                foreach (var obj in list)
                {
                    cnt++;
                    var upper = 0 == obj.Parent ? null : PermissionInstance.Find(f => f.id == obj.Parent);
                    var id    = Utility.UrlEncode(Utility.Encrypt(obj.id.ToString()));
                    html += "<tr>" +
                            "<td style=\"width: 40px; text-align: center;\"><input type=\"checkbox\" id=\"cb_" +
                            id + "\" /></td>" +
                            "<td style=\"width: 40px; text-align: center;\">" + cnt + "</td>" +
                            "<td style=\"width: 150px;\"><a href=\"./permission_add.aspx?key=" +
                            id + "\" title=\"Edit\">" + obj.Name + "</a></td>" +
                            "<td style=\"width: 40px;\">" + ("" == obj.Image ? "-" : ("<img alt=\"\" src=\"" + obj.Image + "\" />")) + "</td>" +
                            "<td style=\"width: 60px;\">" + (obj.IsDefault.Value ? ("<img alt=\"\" src=\"../images/check_hover.png\" />") : "-") + "</td>" +
                            "<td><a href=\"#p" + (0 == obj.Parent ? "" : obj.Parent.ToString()) + "\" title=\"查询本类页面列表\">" +
                            (null == upper ? "" : upper.Name) + "</a></td>" +
                            "<td>" + obj.Url + "</td>" +
                            "<td>" + obj.Description + "</td>" +
                            "</tr>";
                }
            }
            tbodyBody.InnerHtml  = html;
            divPagging.InnerHtml = "";
            if (totalRecords > 0)
            {
                ShowPaggings(pageIndex, totalPages, totalRecords, "./permission_list.aspx", divPagging);
            }
        }
Example #6
0
        protected void bt_Delete_Click(object sender, EventArgs e)
        {
            if ("" != hidID.Value)
            {
                var ids  = GetIdList(hidID.Value.Split(new char[] { ',' }));
                var list = PermissionInstance.FindList(f => ids.Contains(f.id));
                foreach (var tmp in list)
                {
                    tmp.Delete = true;
                    Update(tmp);

                    var his = new TB_AccountHistory();
                    his.Account  = Account.id;
                    his.ActionId = ActionInstance.Find(f => f.Name.Equals("DeletePermission")).id;
                    his.Ip       = Utility.GetClientIP(this.Context);
                    his.ObjectA  = "[id=" + tmp.id + "] " + tmp.Name;
                    SaveHistory(his);
                }
                ShowNotification("./permission_list.aspx", "Success: You have delete " + ids.Count() + " permission(s).");
            }
        }
Example #7
0
        /// <summary>
        /// 更改显示顺序
        /// </summary>
        /// <param name="toupper">true=往上调,false=往下调</param>
        private void ChangeDisplayOrder(bool toupper)
        {
            var id       = int.Parse(Utility.Decrypt(hidID.Value));
            var obj      = PermissionInstance.Find(f => f.id == id);
            var brothers = PermissionInstance.FindList(f => f.Parent == obj.Parent).OrderBy(o => o.DisplayOrder);

            if (obj.DisplayOrder == 0)
            {
                // 原始顺序在第一位时,只有向下调
                if (!toupper)
                {
                    var t = brothers.FirstOrDefault(f => f.DisplayOrder == obj.DisplayOrder + 1);
                    ChangeDisplayOrder(obj, t, toupper);
                }
            }
            else if (obj.DisplayOrder == brothers.Count() - 1)
            {
                // 原始顺序在最后一位时,只有向上调
                if (toupper)
                {
                    var t = brothers.FirstOrDefault(f => f.DisplayOrder == obj.DisplayOrder - 1);
                    ChangeDisplayOrder(obj, t, toupper);
                }
            }
            else
            {
                var t = brothers.FirstOrDefault(f => f.DisplayOrder == (toupper ? (obj.DisplayOrder - 1) : (obj.DisplayOrder + 1)));
                ChangeDisplayOrder(obj, t, toupper);
            }

            var his = new TB_AccountHistory();

            his.Account  = Account.id;
            his.ActionId = ActionInstance.Find(f => f.Name.Equals("EditPermission")).id;
            his.Ip       = Utility.GetClientIP(this.Context);
            his.ObjectA  = "[id=" + obj.id + "] " + obj.Name + ", change display order to " + (toupper ? "lower" : "upper");
            SaveHistory(his);

            ShowNotification("./permission_list.aspx", "Success: You have changed the display order of " + obj.Name + ".");
        }
Example #8
0
        private void NewRole()
        {
            var role = new TB_Role();

            role.AddTime         = DateTime.Now;
            role.Description     = txtDescription.Value.Trim();
            role.IsAdministrator = cbIsAdmin.Checked;
            role.IsDefault       = cbIsDefault.Checked;
            role.Name            = txtName.Value.Trim();
            role.Delete          = false;
            role.Permission      = PermissionInstance.GetDefaultMenus();
            RoleInstance.Add(role);

            // 记录历史
            var his = new TB_AccountHistory();

            his.Account  = Account.id;
            his.ActionId = ActionInstance.Find(f => f.Name.Equals("AddRole")).id;
            his.Ip       = Utility.GetClientIP(this.Context);
            his.ObjectA  = "[id=" + role.id + "] " + role.Name;
            SaveHistory(his);

            ShowNotification("./role_list.aspx", "Success: You added a new role.", true);
        }