示例#1
0
        public ActionResult Create(FormCollection formCollection)
        {
            try
            {
                PermissionMapRepository ml = new PermissionMapRepository();

                PermissionMap obj = new PermissionMap()
                {
                    CreateDate = DateTime.Now, CreateUserID = ID, IsDeleted = false
                };

                UpdateModel(obj);

                bool result = ml.Insert(obj) > 0 ? true : false;

                return(result ? Content(ContentIcon.Succeed + "|操作成功|/Admin/PermissionMap/Index") : Content(ContentIcon.Error + "|操作失败"));
            }
            catch (Exception ex)
            {
                return(Content(ContentIcon.Error + "|" + ErrorWirter(RouteData, ex.Message)));
            }
        }
        /// <summary>
        /// 自动生成菜单及权限结构
        /// </summary>
        /// <param name="title"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        private static void AutoGenerateMenuAndMap(string title, string url)
        {
            DBContext db = new DBContext();

            //分析当前地址
            url = url.Substring(0, url.LastIndexOf("/") + 1) + "Index";
            int menuID = 0;

            //寻找菜单
            try
            {
                menuID = db.FromSql("SELECT * FROM [Menu] AS m WHERE [Url] LIKE '" + url.ToLower() + "'").ToFirst <Menu>().ID;
                if (menuID < 1)
                {
                    throw new Exception("菜单中不存在则新建");
                }
            }
            catch
            {
                try
                {
                    //若当前数据库中不存在该菜单,则自动创建一个菜单
                    var mt = new Menu()
                    {
                        Name             = title,
                        ParentID         = 0,
                        Icon             = "icos-list-images",
                        Url              = url,
                        Sort             = 0,
                        Level            = 1,
                        IsDeleted        = false,
                        LastUpdateDate   = DateTime.Now,
                        CreateDate       = DateTime.Now,
                        CreateUserID     = CurrentMember.ID,
                        LastUpdateUserID = CurrentMember.ID
                    };
                    MenuRepository ml = new MenuRepository();
                    menuID = ml.Insert(mt);
                }
                catch { }
            }
            //权限结构
            var  pmt    = new PermissionMap();
            bool hasMap = false;

            try
            {
                pmt = db.FromSql("SELECT * FROM [PermissionMap] AS pm WHERE [MID]=" + menuID + " AND [Name]='" + title + "'").ToFirst <PermissionMap>();
                if (pmt != null && pmt.ID > 0)
                {
                    hasMap = true;
                }
            }
            catch { }
            if (hasMap == false)
            {
                //自动产生权限结构
                pmt = new PermissionMap()
                {
                    SortID           = GetSort(title),
                    MID              = menuID,
                    Name             = title,
                    Description      = title,
                    IsBasic          = 0,
                    CreateUserID     = CurrentMember.ID,
                    CreateDate       = DateTime.Now,
                    LastUpdateDate   = DateTime.Now,
                    LastUpdateUserID = CurrentMember.ID,
                    IsDeleted        = false
                };
                var pml = new PermissionMapRepository();
                pml.Insert(pmt);
            }
        }