tbf/Common/CurrentUser.cs

133 lines
4.3 KiB
C#
Raw Normal View History

///
/// 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<UserAndForm> userStack = new Stack<UserAndForm>();
public static Common.AuthorizedAs AuthorizedAs;
public static DateTime LastAuthorization = DateTime.Now;
/// <summary>
/// Push a new user into the stack of users and form references.
/// Prevent double user stack entry for the same Windows form.
/// </summary>
/// <param name="newUser">New user</param>
/// <param name="currentForm">Reference to the current form</param>
public static void Change(IUser newUser, Form currentForm)
{
if (userStack.Count > 0 && userStack.Peek().Form == currentForm)
{
userStack.Pop();
}
userStack.Push(new UserAndForm(newUser, currentForm));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="formBeingClosed">Reference to the form currently being closed</param>
/// <returns>true = current user was restored, false = no current user change</returns>
public static bool Restore(Form formBeingClosed)
{
if (userStack.Count > 0 && userStack.Peek().Form == formBeingClosed)
{
userStack.Pop();
return true;
}
return false;
}
/// <summary>
/// Return the user currenty at the top of the stack of users.
/// </summary>
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;
/// <summary>
/// Return the current user name or an empty string
/// </summary>
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);
}
/// <summary>
/// Return the current user number or 0
/// </summary>
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);
}
}
}