/// /// Copyright (c) 2021-2022 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Windows.Forms; namespace Common { class UserAndForm { public readonly IUser User; public readonly Form Form; public UserAndForm(IUser user, Form form) { User = user; Form = form; } } public static class CurrentUser { public const string AdminUsername = "admin"; public const string AdminPassword = "staratura"; public static int MinPasswdLength = 0; public static int PasswdExpirationPeriodDays = 0; public static DBSettings RemoteUsersDB = null; public static DBSettings LocalUsersDB = null; static readonly Stack userStack = new Stack(); public static Common.AuthorizedAs AuthorizedAs; public static DateTime LastAuthorization = DateTime.Now; /// /// Push a new user into the stack of users and form references. /// Prevent double user stack entry for the same Windows form. /// /// New user /// Reference to the current form public static void Change(IUser newUser, Form currentForm) { if (userStack.Count > 0 && userStack.Peek().Form == currentForm) { userStack.Pop(); } userStack.Push(new UserAndForm(newUser, currentForm)); } /// /// Restore an original user from the stack of users - remove user at the top of the stack. /// Prevent restoring when there is no entry for the form currently being closed. /// /// Reference to the form currently being closed /// true = current user was restored, false = no current user change public static bool Restore(Form formBeingClosed) { if (userStack.Count > 0 && userStack.Peek().Form == formBeingClosed) { userStack.Pop(); return true; } return false; } /// /// Return the user currenty at the top of the stack of users. /// public static IUser User() { return (userStack.Count > 0) ? userStack.Peek().User : null; /// TODO: Return default user when stack is empty } public static IUser User2; public static IUser User3; /// /// Return the current user name or an empty string /// public static string UserName() { if (User() == null || User().UserName == null) return string.Empty; else if (User2 == null || User2.UserName == null) return User().UserName; else if (User3 == null || User3.UserName == null) return string.Format("{0},{1}", User().UserName, User2.UserName); else return string.Format("{0},{1},{2}", User().UserName, User2.UserName, User3.UserName); } public static string GetEncodedString(string legalizator) { if (User() == null || User().UserName == null) return string.Empty; return string.Format("{0}~{1}~{2}", User().UserName, User().FullName == null ? string.Empty : User().FullName, legalizator == null ? string.Empty : legalizator); } /// /// Return the current user number or 0 /// public static int Number() { return (User() != null) ? User().Number : 0; } public static bool IsPowerUser() { return (User() != null) ? User().IsPowerUser() : false; } public static bool IsMemberOf(GID groupId) { return (User() != null) ? User().IsMemberOf(groupId) : false; } public static bool IsMemberOf(GID[] groupIds) { return (User() != null) ? User().IsMemberOf(groupIds) : (groupIds == null); } } }