using System; using System.Net; using System.Collections; using System.Text; using System.IO; using Codice.Client.Common; using Codice.CM.Common; namespace Codice.Client.BaseCommands { public class AclInfoPrinter { public void CalcAclInfo(AclInfo info, string server, WorkspaceInfo wkInfo, bool isInherited) { SpecGenerator spg = new SpecGenerator(); if( (info.Entries != null) && (info.Entries.Length > 0) ) { string inheritedFrom = spg.GetSpecFromObjectInfo(info.CreatorObject, server, wkInfo); foreach( AclEntry entry in info.Entries ) { CalcPermissions( entry, entry.Perm.OverrideGranted, false, false, isInherited, inheritedFrom); CalcPermissions( entry, entry.Perm.OverrideDenied, false, false, isInherited, inheritedFrom); CalcPermissions( entry, entry.Perm.Granted, true, false, isInherited, inheritedFrom); CalcPermissions( entry, entry.Perm.Denied, false, true, isInherited, inheritedFrom); } } if( (info.Inheritance != null) && (info.Inheritance.Length > 0) ) { foreach( AclInfo inh in info.Inheritance ) CalcAclInfo(inh, server, wkInfo, true); } } public string CalcExtendedAclInfo(AclInfo info, int indent, string server, WorkspaceInfo wkInfo) { SpecGenerator spg = new SpecGenerator(); StringBuilder result = new StringBuilder(); string indentstr = "{0,-"+ indent +"}"; result.Append(string.Format(indentstr + "ACL: {1}\n", "", info.AclId)); indent += 2; indentstr = "{0,-"+ indent +"}"; result.Append(string.Format(indentstr + "Creator {1}\n", " ", spg.GetSpecFromObjectInfo(info.CreatorObject, server, wkInfo))); if( (info.Entries != null) && (info.Entries.Length > 0) ) { result.Append(string.Format(indentstr + "Entries\n", " ")); foreach( AclEntry entry in info.Entries ) { CalcAclEntry(result, indentstr, entry); } } if( (info.Inheritance != null) && (info.Inheritance.Length > 0) ) { result.Append(string.Format(indentstr + "Inherited\n", " ")); foreach( AclInfo inh in info.Inheritance ) result.Append(CalcExtendedAclInfo(inh, indent + 2, server, wkInfo)); } return result.ToString(); } public static string GetPrintableSeidName(SEID seidKey) { string name; if (seidKey.Equals(SEIDConsts.EVERYBODY_SEID)) { return SEIDConsts.EVERYBODY_SEID_NAME; } if (seidKey.Equals(SEIDConsts.OWNER_SEID)) { return SEIDConsts.OWNER_SEID_NAME; } try { name = UserInfo.Get().GetNameFromSEID(seidKey); } catch { //Seid cannot be resolved name = seidKey.Data; } return name; } public string GetString() { StringBuilder result = new StringBuilder(); result.Append( string.Format(STR_FORMAT, Localization.GetString("USER_KEY"), Localization.GetString("PERMISSION_KEY"), Localization.GetString("ALLOWED_KEY"), Localization.GetString("DENIED_KEY"), Localization.GetString("INHERITED_FROM_KEY"))); foreach (PrintableEntry entry in mTable.Values) { GetEntryString(result, entry); } return result.ToString(); } public SortedList GetEntries() { return mTable; } private void CalcAclEntry(StringBuilder result, string indentstr, AclEntry entry) { result.Append(string.Format(indentstr + " {1}:\n", " ", GetPrintableSeidName(entry.Seid))); if (entry.Perm.Granted != Permissions.NO_PERMISSIONS) { result.Append(string.Format(indentstr + " Allowed:\n", " ")); result.Append(string.Format(indentstr + " {1}\n", " ", GetString(ClientPermissions.GetPermissionNames(entry.Perm.Granted)))); } if (entry.Perm.Denied != Permissions.NO_PERMISSIONS) { result.Append(string.Format(indentstr + " Denied:\n", " ")); result.Append(string.Format(indentstr + " {1}\n", " ", GetString(ClientPermissions.GetPermissionNames(entry.Perm.Denied)))); } if (entry.Perm.OverrideGranted != Permissions.NO_PERMISSIONS) { result.Append(string.Format(indentstr + " Override Allowed:\n", " ")); result.Append(string.Format(indentstr + " {1}\n", " ", GetString(ClientPermissions.GetPermissionNames(entry.Perm.OverrideGranted)))); } if (entry.Perm.OverrideDenied != Permissions.NO_PERMISSIONS) { result.Append(string.Format(indentstr + " Override Denied:\n", " ")); result.Append(string.Format(indentstr + " {1}\n", " ", GetString(ClientPermissions.GetPermissionNames(entry.Perm.OverrideDenied)))); } } private void CalcPermissions( AclEntry entry, Permissions permissions, bool bGranted, bool bDenied, bool isInherited, string inheritedFrom) { ArrayList permNames = (ArrayList)ClientPermissions.GetPermissionNames(permissions); foreach (string perm in permNames) { PrintableEntry aclEntry = new PrintableEntry( GetPrintableSeidName(entry.Seid), perm, bGranted, bDenied, isInherited, inheritedFrom); Add(aclEntry); } } private void Add(PrintableEntry entry) { string key = entry.user + entry.permission + entry.inherited; if (mTable[key] == null) { mTable.Add(key, entry); return; } (mTable[key] as PrintableEntry).denied = entry.denied; (mTable[key] as PrintableEntry).granted = entry.granted; } private string GetString(IList list) { string result = string.Empty; if (list.Count == 0) return string.Empty; foreach (string s in list) { result += s + " "; } return result; } private void GetEntryString(StringBuilder result, PrintableEntry entry) { if (entry.granted) { result.Append(string.Format(STR_FORMAT, entry.user, entry.permission, Localization.GetString("True"), "", entry.inherited)); return; } if (entry.denied) { result.Append(string.Format(STR_FORMAT, entry.user, entry.permission, "", Localization.GetString("True"), entry.inherited)); return; } result.Append(string.Format(STR_FORMAT, entry.user, entry.permission, "", "", entry.inherited)); } private const string STR_FORMAT = "{0,-12}{1,-15} {2,-7} {3,-6} {4,-15}\n"; //Table with ACL entries for print //The key is User+Permission+inherited private SortedList mTable = new SortedList(); } public class PrintableEntry { public string user; public string permission; public bool granted; public bool denied; public string inherited; public PrintableEntry ( string user, string permission, bool granted, bool denied, bool inherited, string inheritedFrom) { this.user = user; this.permission= permission; this.granted = granted; this.denied = denied; if( inherited ) this.inherited = inheritedFrom; else this.inherited = "--"; } } }