using System; using System.Collections.Generic; using System.Text; using ScrewTurn.Wiki.PluginFramework; namespace ScrewTurn.Wiki { /// /// Implements tools supporting athorization management. /// public static class AuthTools { /// /// Determines whether an action is valid. /// /// The action to validate. /// The list of valid actions. /// true if the action is valid, false otherwise. public static bool IsValidAction(string action, string[] validActions) { return Array.Find(validActions, delegate(string s) { return s == action; }) != null; } /// /// Determines whether a subject is a group. /// /// The subject to test. /// true if the subject is a group, false if it is a user. public static bool IsGroup(string subject) { if(subject == null) throw new ArgumentNullException("subject"); if(subject.Length < 2) throw new ArgumentException("Subject must contain at least 2 characters", "subject"); return subject.ToUpperInvariant().StartsWith("G."); } /// /// Prepends the proper string to a username. /// /// The username. /// The resulting username. public static string PrepareUsername(string username) { if(username == null) throw new ArgumentNullException("username"); if(username.Length == 0) throw new ArgumentException("Username cannot be empty", "username"); return "U." + username; } /// /// Prepends the proper string to each group name in an array. /// /// The group array. /// The resulting group array. public static string[] PrepareGroups(string[] groups) { if(groups == null) throw new ArgumentNullException("groups"); if(groups.Length == 0) return groups; string[] result = new string[groups.Length]; for(int i = 0; i < groups.Length; i++) { if(groups[i] == null) throw new ArgumentNullException("groups"); if(groups[i].Length == 0) throw new ArgumentException("Groups cannot contain empty elements", "groups"); result[i] = PrepareGroup(groups[i]); } return result; } /// /// Prepends the proper string to the group name. /// /// The group name. /// The result string. public static string PrepareGroup(string group) { return "G." + group; } /// /// Gets the proper full name for a directory. /// /// The provider. /// The directory name. /// The full name (not prepended with . public static string GetDirectoryName(IFilesStorageProviderV30 prov, string name) { if(prov == null) throw new ArgumentNullException("prov"); if(name == null) throw new ArgumentNullException("name"); if(name.Length == 0) throw new ArgumentException("Name cannot be empty", "name"); return "(" + prov.GetType().FullName + ")" + name; } } }