RequiredGroupMembership: Users.Grp.GID --> Users.Grp.GID[] (initial version), ver. 2.18.1006

This commit is contained in:
Milan Hanajik
2018-09-25 09:05:51 +02:00
parent 438374532f
commit cd246e89c8
22 changed files with 130 additions and 113 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
///
/// Copyright (c) 2016-2017 Sensus Metering Systems
/// Copyright (c) 2016-2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
+59 -64
View File
@@ -1,5 +1,5 @@
///
/// Copyright (c) 2016-2017 Sensus Metering Systems
/// Copyright (c) 2016-2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
@@ -101,6 +101,38 @@ namespace Users.Entities
}
}
/// <summary>
/// Check whether user is a member of any of specified groups.
/// Returns true also when no groups are defined (groupIds = null).
/// </summary>
/// <param name="groupIds">An array of group GID-s or null (no membership required)</param>
/// <returns>true if user is a member of any of specified groups</returns>
public virtual bool IsMemberOf(Grp.GID[] groupIds)
{
if (groupIds == null || powerUser)
{
return true;
}
foreach (var groupId in groupIds)
{
if (groupId >= 0 && (int)groupId < Grp.Count)
{
// for all group elements in User.groups
for (int i = 0; i < Groups.Count; ++i)
{
// element GID = parameter GroupId ?
if (((Group)Groups[i]).Gid == (int)groupId)
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// Sets users password. It then will be encrypted.
/// </summary>
@@ -129,17 +161,17 @@ namespace Users.Entities
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid name and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only name and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and access rights were not
/// risen to a higher level. If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="userName">User name</param>
/// <param name="password">Password</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool Authorize(string userName, string password, Grp.GID requiredGroupMembership)
public virtual bool Authorize(string userName, string password, Grp.GID[] requiredGroupMembership)
{
if (IsPowerUser(userName, password))
{
@@ -167,26 +199,17 @@ namespace Users.Entities
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool Authorize(string userName, string password)
{
return Authorize(userName, password, Grp.GID.None);
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid number and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only number and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and access rights were not
/// risen to a higher level. If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="number">User ID number</param>
/// <param name="password">Password</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeNumber(int number, string password, Grp.GID requiredGroupMembership)
public virtual bool AuthorizeNumber(int number, string password, Grp.GID[] requiredGroupMembership)
{
if (Number == number)
{
@@ -206,26 +229,17 @@ namespace Users.Entities
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool AuthorizeNumber(int number, string password)
{
return AuthorizeNumber(number, password, Grp.GID.None);
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid name and password and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only name and password must be valid.
/// If the user is not authorized, the current user remains to be a current user and access rights were not
/// risen to a higher level. If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="fullName"></param>
/// <param name="password"></param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeFullName(string fullName, string password, Grp.GID requiredGroupMembership)
public virtual bool AuthorizeFullName(string fullName, string password, Grp.GID[] requiredGroupMembership)
{
if (FullName.ToLower() == fullName.ToLower())
{
@@ -244,26 +258,17 @@ namespace Users.Entities
return false;
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool AuthorizeFullName(string fullName, string password)
{
return AuthorizeFullName(fullName, password, Grp.GID.None);
}
/// <summary>
/// The FIRST of TWO possible user authorization method to be used
/// when a specific group membership is required (you can use Grp.GID.None).
/// If the user is not authorized, the current user remains to be a current user
/// (i.e. the access rights were not risen to a higher level).
/// If you require different behavior, use Unauthorize() before calling Authorize().
/// Authorization method: 'requiredGroupMembership' contains a list of required groups.
/// Valid TAG and a mbership in any of theese goups grants access, Authorize(..) returns true.
/// When requiredGroupMembership == null, no membership is required, only the TAG must be valid.
/// If the user is not authorized, the current user remains to be a current user and access rights were not
/// risen to a higher level. If you require different behavior, use Unauthorize() before calling Authorize().
/// </summary>
/// <param name="tag">Tag (RFID, NFC, ... s/n)</param>
/// <param name="requiredGrupMembership"></param>
/// <returns>true = authorized</returns>
public virtual bool AuthorizeTag(string tag, Grp.GID requiredGroupMembership)
public virtual bool AuthorizeTag(string tag, Grp.GID[] requiredGroupMembership)
{
if (Tag == tag)
{
@@ -282,16 +287,6 @@ namespace Users.Entities
return false;
}
/// <summary>
/// The SECOND of TWO possible user authorization method to be used when
/// no specific group membership is required.
/// </summary>
public virtual bool AuthorizeTag(string tag)
{
return AuthorizeTag(tag, Grp.GID.None);
}
/// <summary>
/// Returns a 'User' with a given username from a database.
/// </summary>
+20 -9
View File
@@ -18,8 +18,9 @@ namespace Users.Forms
// Private fields
string user;
string password;
Grp.GID requiredGroupMembership = Grp.GID.Invalid;
Grp.GID[] requiredGroupMembership;
bool noDatabase;
Color oriBackColor;
/// Smart card support, card S/N is used as user.Tag
GemCard.CardNative card;
@@ -35,6 +36,8 @@ namespace Users.Forms
public LoginDlg()
{
InitializeComponent();
oriBackColor = BackColor;
}
/// <summary>
@@ -59,16 +62,21 @@ namespace Users.Forms
/// <summary>
/// Constructor when a specific group membership is required.
/// </summary>
public LoginDlg(Grp.GID requiredGroupMembership)
public LoginDlg(Grp.GID[] requiredGroupMembership)
: this()
{
this.requiredGroupMembership = requiredGroupMembership;
#if DEBUG
user = "MHA";
userNameTextBox.Text = "MHA";
passwordTextBox.Text = "kremik";
#endif
}
/// <summary>
/// Constructor with a predefined user when a specific group membership is required.
/// </summary>
public LoginDlg(string predefinedUser, Grp.GID requiredGroupMembership)
public LoginDlg(string predefinedUser, Grp.GID[] requiredGroupMembership)
: this()
{
user = predefinedUser;
@@ -85,6 +93,9 @@ namespace Users.Forms
else
passwordTextBox.Select();
///
/// Smart card reader detection
///
smartCardReader = null; /// null = No smart card reader detected
#if TURA_IPERL || TURA_SPECIAL
@@ -183,15 +194,15 @@ namespace Users.Forms
{
default:
case Entities.LoginMethod.UserName:
authorized = (requiredGroupMembership == Grp.GID.Invalid) ? loadedUser.Authorize(user, password) : loadedUser.Authorize(user, password, requiredGroupMembership);
authorized = loadedUser.Authorize(user, password, requiredGroupMembership);
break;
case Entities.LoginMethod.FullName:
authorized = (requiredGroupMembership == Grp.GID.Invalid) ? loadedUser.AuthorizeFullName(user, password) : loadedUser.AuthorizeFullName(user, password, requiredGroupMembership);
authorized = loadedUser.AuthorizeFullName(user, password, requiredGroupMembership);
break;
case Entities.LoginMethod.Number:
int number;
if (!int.TryParse(user, out number)) break;
authorized = (requiredGroupMembership == Grp.GID.Invalid) ? loadedUser.AuthorizeNumber(number, password) : loadedUser.AuthorizeNumber(number, password, requiredGroupMembership);
authorized = loadedUser.AuthorizeNumber(number, password, requiredGroupMembership);
break;
}
}
@@ -222,7 +233,7 @@ namespace Users.Forms
/// </summary>
private void cancelButton_Click(object sender, EventArgs e)
{
if (requiredGroupMembership == Grp.GID.Invalid)
if (requiredGroupMembership == null)
{
Entities.User.Unauthorize();
}
@@ -278,7 +289,7 @@ namespace Users.Forms
Users.Entities.User loadedUser = Users.Entities.User.LoadUserByTag(tag, db);
if (loadedUser != null)
{
authorized = (requiredGroupMembership == Grp.GID.Invalid) ? loadedUser.AuthorizeTag(tag) : loadedUser.AuthorizeTag(tag, requiredGroupMembership);
authorized = loadedUser.AuthorizeTag(tag, requiredGroupMembership);
}
}
catch (Exception) { }
@@ -304,7 +315,7 @@ namespace Users.Forms
void OnCardRemoved(object sender, CardRemovedEventArgs args)
{
BackColor = SystemColors.Control;
BackColor = oriBackColor;
}
private void LoginDlg_FormClosing(object sender, FormClosingEventArgs e)
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.18.982.0")]
[assembly: AssemblyFileVersion("2.18.982.0")]
[assembly: AssemblyVersion("2.18.1006.0")]
[assembly: AssemblyFileVersion("2.18.1006.0")]