示例#1
0
        /// <summary>
        /// Sync actions with Permission object
        /// </summary>
        public void SyncPermissionWithAction()
        {
            Type permission=typeof(Permission);
            FieldInfo[] fields = permission.GetFields();
            if (fields == null || fields.Length<=0)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                foreach (FieldInfo field in fields)
                {
                    var action = from a in db.Admin_Action where a.action_name == field.Name select a;
                    if (action == null || action.ToList<Admin_Action>().Count == 0)
                    {
                        Admin_Action new_action = new Admin_Action();
                        new_action.action_name = field.Name;
                        new_action.action_description = field.Name;
                        new_action.enable = true;
                        db.Admin_Action.Add(new_action);
                    }
                }
                db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {

            }
            finally
            {
                db.Dispose();
            }
        }
示例#2
0
        /// <summary>
        /// Sync actions with Permission object
        /// </summary>
        public static void SyncPermissionWithAction()
        {
            Type permission = typeof(Permission);
            FieldInfo[] fields = permission.GetFields();
            if (fields == null || fields.Length <= 0)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                List<AdminActionAttribute> cates=new List<AdminActionAttribute>();
                List<Admin_Action> allActions=(from action in db.Admin_Action select action).ToList<Admin_Action>();
                List<Admin_Category> allCates=(from cate in db.Admin_Category select cate).ToList<Admin_Category>();
                foreach (FieldInfo field in fields)
                {
                    AdminActionAttribute attr = field.GetCustomAttribute<AdminActionAttribute>();
                    Admin_Action action = (from a in allActions where a.action_name == field.Name select a).FirstOrDefault<Admin_Action>();
                    if (action == null)
                    {
                        action = new Admin_Action();
                        action.action_name = field.Name;
                        action.enable = true;
                        db.Admin_Action.Add(action);
                    }

                    if (attr != null)
                    {
                        action.category_id = attr.ID;
                        action.action_description = attr.ActionDescription;
                        AdminActionAttribute existed = (from pcate in cates where pcate.ID == attr.ID select pcate).FirstOrDefault<AdminActionAttribute>();
                        if (existed == null)
                        {
                            cates.Add(attr);
                        }
                    }
                }
                db.SaveChanges();

                foreach (Admin_Action action in allActions)
                {
                    bool found = false;

                    foreach (FieldInfo field in fields)
                    {
                        if (action.action_name == field.Name)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        action.enable = false;
                        //db.Admin_Action.Remove(action);
                    }
                }

                db.SaveChanges();

                //category
                foreach (AdminActionAttribute pcate in cates)
                {
                    Admin_Category dbCate=(from c in allCates where c.ID==pcate.ID select c).FirstOrDefault<Admin_Category>();
                    if (dbCate == null)
                    {
                        dbCate = new Admin_Category();
                        dbCate.ID = pcate.ID;
                        dbCate.Name = pcate.CategoryName;
                        dbCate.System_category = pcate.IsSystem;
                        dbCate.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Admin_Category.Add(dbCate);
                    }
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                db.Dispose();
            }
        }