2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2016-12-21 07:06:42 +00:00
|
|
|
|
/// Copyright (c) 2013-2016 Sensus Metering Systems
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
using TBF.Resources;
|
|
|
|
|
|
|
2016-12-21 07:06:42 +00:00
|
|
|
|
namespace TBF.Forms
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides login dialog as well as user authorization using TBF.User.Authorize(...)
|
|
|
|
|
|
/// (i.e. when DialogResult is OK, user was authorized).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class LoginDlg : Form
|
|
|
|
|
|
{
|
|
|
|
|
|
// Private fields
|
|
|
|
|
|
string user;
|
|
|
|
|
|
string password;
|
2015-12-04 16:17:20 +00:00
|
|
|
|
Config.Grp.GID requiredGroupMembership = Config.Grp.GID.Invalid;
|
2015-02-19 14:50:31 +00:00
|
|
|
|
bool noDatabase;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public LoginDlg()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor with a predefined user.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public LoginDlg(string predefinedUser)
|
|
|
|
|
|
: this()
|
|
|
|
|
|
{
|
|
|
|
|
|
user = predefinedUser;
|
|
|
|
|
|
userNameTextBox.Text = predefinedUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-19 14:50:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor with 'no Database' flag.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public LoginDlg(bool noDatabase)
|
|
|
|
|
|
: this()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.noDatabase = noDatabase;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor when a specific group membership is required.
|
|
|
|
|
|
/// </summary>
|
2015-12-04 16:17:20 +00:00
|
|
|
|
public LoginDlg(Config.Grp.GID requiredGroupMembership)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
: this()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.requiredGroupMembership = requiredGroupMembership;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor with a predefined user when a specific group membership is required.
|
|
|
|
|
|
/// </summary>
|
2015-12-04 16:17:20 +00:00
|
|
|
|
public LoginDlg(string predefinedUser, Config.Grp.GID requiredGroupMembership)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
: this()
|
|
|
|
|
|
{
|
|
|
|
|
|
user = predefinedUser;
|
|
|
|
|
|
userNameTextBox.Text = predefinedUser;
|
|
|
|
|
|
this.requiredGroupMembership = requiredGroupMembership;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void LoginDlg_Load(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2015-12-20 12:26:33 +00:00
|
|
|
|
Text = Strings.User_Login + " TBF v." + Program.Version;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
label1.Text = Strings.Username;
|
|
|
|
|
|
label2.Text = Strings.Password;
|
|
|
|
|
|
okButton.Text = Strings.OkBtnText;
|
|
|
|
|
|
cancelButton.Text = Strings.CancelBtnText;
|
|
|
|
|
|
|
|
|
|
|
|
if (user == null) userNameTextBox.Select();
|
|
|
|
|
|
else passwordTextBox.Select();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Try to authorize the user when OK clicked.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
user = userNameTextBox.Text;
|
|
|
|
|
|
password = passwordTextBox.Text;
|
|
|
|
|
|
|
2015-12-04 16:17:20 +00:00
|
|
|
|
bool authorized = Config.Entities.User.IsPowerUser(user, password);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-02-19 14:50:31 +00:00
|
|
|
|
if (!authorized && !noDatabase)
|
|
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
Config.Entities.User loadedUser = Config.Entities.User.LoadUserByName(user);
|
2015-02-19 14:50:31 +00:00
|
|
|
|
if (loadedUser != null)
|
|
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (requiredGroupMembership == Config.Grp.GID.Invalid)
|
2015-02-19 14:50:31 +00:00
|
|
|
|
{
|
|
|
|
|
|
authorized = loadedUser.Authorize(user, password);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
authorized = loadedUser.Authorize(user, password, requiredGroupMembership);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-02-19 14:50:31 +00:00
|
|
|
|
if (authorized)
|
|
|
|
|
|
{
|
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
|
Close();
|
|
|
|
|
|
if (Program.MainWnd != null) Program.MainWnd.UpdateUser();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
MessageBox.Show(Strings.Invalid_username_or_password, Strings.Error, MessageBoxButtons.OK);
|
|
|
|
|
|
DialogResult = DialogResult.None;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Cancel clicked, do not try to authorize.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (requiredGroupMembership == Config.Grp.GID.Invalid)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
Config.Entities.User.Unauthorize();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
|
Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|