public static AuthorizedAction FindAction(AuthorizedAction parentAction, string subaction)
        {
            var i      = subaction.IndexOf(".");
            var action = subaction;

            if (i > 0)
            {
                action    = subaction.Substring(0, i);
                subaction = subaction.Substring(i + 1);
            }
            else
            {
                subaction = null;
            }
            var ac = parentAction.SubActions.FirstOrDefault(a => a.Name == action);

            if (subaction != null)
            {
                return(FindAction(ac, subaction));
            }
            else
            {
                return(ac);
            }
        }
        public static void RegisterSiteMapActions()
        {
            AuthorizedAction ac = new AuthorizedAction("Menu");

            ac.Title = "منو";
            actions.SubActions.Add(ac);
            AddActionsFromSiteMap(ac);
        }
        public static AuthorizedAction RegisterAction(string action, params string[] subActions)
        {
            AuthorizedAction ac = actions.AddSubAction(action);

            foreach (string s in subActions)
            {
                ac.AddSubAction(s);
            }
            return(ac);
        }
        public static void AddActionsFromSiteMap(AuthorizedAction actions)
        {
            actions.SubActions.Clear();
            //HttpContext context = HttpContext.Current;
            XmlDocument doc = new XmlDocument();

            //Page p = (Page)type.GetProperty("Page").GetValue(menu, null);
            doc.Load(HttpContext.Current.Server.MapPath("~/Web.sitemap"));
            AddActionsFromSiteMap(doc.DocumentElement.FirstChild.ChildNodes, actions);
        }
Exemple #5
0
        private AuthorizedAction AddSubActionInternal(string name)
        {
            AuthorizedAction ac = this[name];

            if (ac == null)
            {
                SubActions.Add(ac = new AuthorizedAction(name));
            }
            return(ac);
        }
        //private static void FilterUserAccess(string parentAction, AuthorizedAction actions)
        //{
        //    for (int i = 0; i < actions.SubActions.Count; i++)
        //    {
        //        if(AuthorizationChecker.HasAccess(actions.SubActions[i].FullName, true))

        //    }
        //}

        public static void LocalizeAction(AuthorizedAction action, Func <string, string> localizer)
        {
            if (!string.IsNullOrEmpty(action.Title))
            {
                action.Title = localizer(action.Title);
            }
            foreach (var ac in action.SubActions)
            {
                LocalizeAction(ac, localizer);
            }
        }
Exemple #7
0
        public AuthorizedAction AddSubAction(string name)
        {
            string[]         ss = Utility.SplitString(name, '.');
            AuthorizedAction ac = this;

            foreach (string s in ss)
            {
                ac = ac.AddSubActionInternal(s);
            }
            return(ac);
        }
Exemple #8
0
        public object Clone()
        {
            //    return Clone(false);
            //}
            //public object Clone(bool filterUserAccess)
            //{
            AuthorizedAction ac = new AuthorizedAction(name);

            ac.title = title;
            foreach (var sub in SubActions)
            {
                ac.SubActions.Add((AuthorizedAction)sub.Clone());
            }
            return(ac);
        }
        //TODO: remove duplicate code: GetEnumActions
        public static AuthorizedAction RegisterAction(AuthorizedAction parentAction, Type actionsEnumType, bool onlyAddChilds = false)
        {
            if (!actionsEnumType.IsEnum)
            {
                throw new ArgumentException("پارامتر بايد از نوع شمارشي باشد.", "actionsEnumType");
            }
            AuthorizedAction enumAction;

            if (onlyAddChilds)
            {
                enumAction = parentAction;
            }
            else
            {
                enumAction       = new AuthorizedAction(actionsEnumType.Name);
                enumAction.Title = GetTitle(actionsEnumType, enumAction.Name);
                parentAction.SubActions.Add(enumAction);
            }

            FieldInfo[] fields = actionsEnumType.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo fi in fields)
            {
                if (fi.Name.Contains("_"))
                {
                    var    acs   = fi.Name.Split('_');
                    string title = GetTitle(fi, fi.Name);
                    var    tis   = title.Split('.');
                    var    ac    = enumAction;
                    for (int i = 0; i < acs.Length; i++)
                    {
                        ac       = ac.AddSubAction(acs[i]);
                        ac.Title = tis[i];
                    }
                }
                else
                {
                    AuthorizedAction ac = enumAction.AddSubAction(fi.Name);
                    ac.Title = GetTitle(fi, fi.Name);
                }
            }
            return(enumAction);
        }
 private static void AddActionsFromSiteMap(XmlNodeList nodes, AuthorizedAction actions)
 {
     foreach (XmlNode node in nodes)
     {
         if (node.NodeType == XmlNodeType.Comment)
         {
             continue;
         }
         string name = "";
         if (node.Attributes["action"] != null)
         {
             name = node.Attributes["action"].Value;
         }
         else if (node.Attributes["url"] != null)
         {
             name = node.Attributes["url"].Value;
             if (name.ToLower().EndsWith(".aspx"))
             {
                 name = name.Remove(name.Length - 5);
             }
             //name = name.Substring(name.LastIndexOf('\\') + 1);
         }
         //if (!name.ToLower().StartsWith("menu."))
         //    name = "Menu." + name;
         name = name.Replace('.', '_');
         AuthorizedAction ac = new AuthorizedAction(name);
         if (node.Attributes["title"] != null)
         {
             ac.Title = node.Attributes["title"].Value;
         }
         else
         {
             ac.Title = name;
         }
         actions.SubActions.Add(ac);
         if (node.HasChildNodes)
         {
             AddActionsFromSiteMap(node.ChildNodes, ac);
         }
     }
 }
Exemple #11
0
        public bool ContainsAction(AuthorizedAction action)
        {
            AuthorizedAction ac = this[action.name];

            if (ac == null)
            {
                return(false);
            }
            if (action.SubActions.Count == 0)
            {
                return(true);
            }
            foreach (AuthorizedAction sub in action.SubActions)
            {
                if (ac.ContainsAction(sub))
                {
                    return(true);
                }
            }
            return(false);
        }
        public static AuthorizedAction GetEnumActions <EnumType>(AuthorizedAction addChildsToThis = null)
        {
            var t = typeof(EnumType);
            AuthorizedAction enumAction;

            if (addChildsToThis == null)
            {
                enumAction       = new AuthorizedAction(t.Name);
                enumAction.Title = GetTitle(t, t.Name);
            }
            else
            {
                enumAction = addChildsToThis;
            }
            FieldInfo[] fields = t.GetFields(BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo fi in fields)
            {
                if (fi.Name.Contains("_"))
                {
                    var    acs   = fi.Name.Split('_');
                    string title = GetTitle(fi, fi.Name);
                    var    tis   = title.Split('.');
                    var    ac    = enumAction;
                    for (int i = 0; i < acs.Length; i++)
                    {
                        ac       = ac.AddSubAction(acs[i]);
                        ac.Title = tis[i];
                    }
                }
                else
                {
                    AuthorizedAction ac = enumAction.AddSubAction(fi.Name);
                    ac.Title = GetTitle(fi, fi.Name);
                }
            }
            return(enumAction);
        }
        static AuthorizationManager()
        {
            actions = new AuthorizedAction("root");
            log     = new Common.Logging.Simple.NoOpLogger(); //LogManager.GetCurrentClassLogger();
            var t = SqlHelper.ExecuteScaler(@"SELECT lower(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'dbo' 
                 AND  TABLE_NAME = 'aspnet_users'");

            if (t is string && (string)t == "aspnet_users")
            {
                rolesTableName = "aspnet_roles";
                usersTableName = "aspnet_users";
                //userRolesTabeName = "aspnet_UsersInRoles";
            }
            else
            {
                rolesTableName = "roles";
                usersTableName = "users";
                //userRolesTabeName = "UsersInRoles";
            }
            userRoleActionsTableName = System.Web.Configuration.WebConfigurationManager.AppSettings["Uniconr.UserRoleActionsTableName"];
            if (userRoleActionsTableName == null)
            {
                userRoleActionsTableName = "aspnet_UserRoleActions";
            }
            if (SqlHelper.ExecuteScaler(@"SELECT * FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'dbo' 
                 AND  TABLE_NAME = '" + userRoleActionsTableName + "'") == null)
            {
                ExecuteScalar(@"CREATE TABLE [dbo].[" + userRoleActionsTableName + @"](
	[ActionName] [nvarchar](max) NOT NULL,
	[UserRoleId] [uniqueidentifier] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
");
            }
        }
 public static AuthorizedAction RegisterAction <EnumType>(AuthorizedAction parentAction, bool onlyAddChilds = false)
 {
     return(RegisterAction(parentAction, typeof(EnumType), onlyAddChilds));
 }