SafeDispatch/SubscriberAndUserManager/UsersForm.cs
2024-02-22 18:43:59 +02:00

344 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using SafeMobileLib;
using Telerik.WinControls.UI;
using System.Linq;
namespace SubscriberAndUserManager
{
public partial class UsersForm : Telerik.WinControls.UI.RadForm
{
private DBuserManager DB;
private DBsettingsManager DBsettings;
private DBgatewaysManager DBgateway;
private DBcontactsManager DBcontact = new DBcontactsManager(MainForm.DBServer, MainForm.DBSchema, MainForm.DBUser, MainForm.DBPass, MainForm.DBPort);
private bool formTypeEdit = false; //false = add user mode; true = edit user mode
private USERTYPE loadType = USERTYPE.LiteDispatcher;
private USERTYPE finalType = USERTYPE.LiteDispatcher;
public static Dictionary<int,UsersForm> UsersCollection = new Dictionary<int, UsersForm>();
private void setLanguage()
{
this.Text = MainForm.returnLNGString("useform");
lbUserType.Text = MainForm.returnLNGString("usetype");
lbLogin.Text = MainForm.returnLNGString("logname");
lbPass.Text = MainForm.returnLNGString("pass");
lbConf.Text = MainForm.returnLNGString("conf");
lbFirstName.Text = MainForm.returnLNGString("firstname");
lbLastName.Text = MainForm.returnLNGString("lastname");
btnExit.Text = MainForm.returnLNGString("exit");
btnAdd.Text = MainForm.returnLNGString("add");
lbBackupUser.Text = MainForm.returnLNGString("backupUser");
cbUserType.Items.Clear();
cbUserType.Items.Add(MainForm.returnLNGString("dispatcher"));
cbUserType.Items.Add(MainForm.returnLNGString("admin"));
cbUserType.Items.Add(MainForm.returnLNGString("liteDispatcher"));
cbUserType.Items.Add(MainForm.returnLNGString("ticketAdmin"));
}
public UsersForm(DBuserManager DB, DBsettingsManager DBsettings, DBgatewaysManager DBgateway)
{
InitializeComponent();
setLanguage();
cbUserType.SelectedIndex = 0;
this.DB = DB;
this.DBsettings = DBsettings;
this.DBgateway = DBgateway;
if (MainForm.radioType==RADIOTYPE.HYT)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.SYTADMIN24x;
this.BackColor = MainForm.HyteraColor;
}
else if (MainForm.radioType == RADIOTYPE.HARRIS)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.visionSmall;
this.BackColor = MainForm.HarrisColor;
this.lbConf.ForeColor = MainForm.HarTextColor;
this.lbFirstName.ForeColor = MainForm.HarTextColor;
this.lbLastName.ForeColor = MainForm.HarTextColor;
this.lbLogin.ForeColor = MainForm.HarTextColor;
this.lbPass.ForeColor = MainForm.HarTextColor;
this.lbUserType.ForeColor = MainForm.HarTextColor;
}
else if (MainForm.radioType == RADIOTYPE.SIMOCO)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.simoco_setup_241;
}
else if (MainForm.radioType == RADIOTYPE.EXCERA)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.AXYS_ICON;
}
else if (MainForm.radioType == RADIOTYPE.ATLAS)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.ATLASadmin32; //ATLASCHANGE
this.BackColor = MainForm.HarrisColor;
this.lbConf.ForeColor = MainForm.HarTextColor;
this.lbFirstName.ForeColor = MainForm.HarTextColor;
this.lbLastName.ForeColor = MainForm.HarTextColor;
this.lbLogin.ForeColor = MainForm.HarTextColor;
this.lbPass.ForeColor = MainForm.HarTextColor;
this.lbUserType.ForeColor = MainForm.HarTextColor;
}
//if ((MainForm.radioType != RADIOTYPE.REPEATER_TRBO) && (MainForm.radioType != RADIOTYPE.MOTO))
// cbUserType.Items.Remove(cbUserType.Items[3]);
//cbUserType.Items.Remove(cbUserType.Items[1]); // removes Administrator from dropdown
}
private User editingUser = null;
public static UsersForm GetSingleton(DBuserManager DB, User usr)
{
if (!UsersCollection.ContainsKey(usr.Id))
UsersCollection.Add(usr.Id, new UsersForm(DB, usr));
else
{
if (UsersCollection[usr.Id].IsDisposed)
UsersCollection[usr.Id] = new UsersForm(DB, usr);
}
return UsersCollection[usr.Id];
}
public UsersForm(DBuserManager DB, User usr)
{
InitializeComponent();
setLanguage();
this.Text = $"{MainForm.returnLNGString("user")} {usr.UserName} {MainForm.returnLNGString("sdset").ToLower()}";
this.DB = DB;
// save editing user
editingUser = usr;
formTypeEdit = true;
btnAdd.Text = MainForm.returnLNGString("edit");
txLogin.Text = usr.UserName;
txFirstName.Text = usr.FirstName;
txLasttName.Text = usr.Lastname;
txPass.Text = usr.Password;
txConfirm.Text = usr.Password;
if (usr.UType == USERTYPE.Dispatcher)
{
cbUserType.SelectedIndex = 0;
}
else if (usr.UType == USERTYPE.LiteDispatcher)
{
cbUserType.SelectedIndex = 2;
}
else if (usr.UType == USERTYPE.TicketingAdmin)
{
if ((MainForm.radioType == RADIOTYPE.REPEATER_TRBO) || (MainForm.radioType == RADIOTYPE.MOTO))
cbUserType.SelectedIndex = 3;
}
else if (usr.UType == USERTYPE.Admin)
{
cbUserType.SelectedIndex = 1;
cbUserType.Enabled = false;
}
if (MainForm.radioType== RADIOTYPE.HYT)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.SYTADMIN24x;
this.BackColor = MainForm.HyteraColor;
}
else if (MainForm.radioType == RADIOTYPE.HARRIS)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.visionSmall;
this.BackColor = MainForm.HarrisColor;
}
else if (MainForm.radioType == RADIOTYPE.SIMOCO)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.simoco_setup_241;
}
else if (MainForm.radioType == RADIOTYPE.EXCERA)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.AXYS_ICON;
}
else if (MainForm.radioType == RADIOTYPE.ATLAS)
{
this.Icon = global::SubscriberAndUserManager.Properties.Resources.ATLASadmin32; //ATLASCHANGE
this.BackColor = MainForm.HarrisColor;
}
//if ((MainForm.radioType != RADIOTYPE.REPEATER_TRBO) && (MainForm.radioType != RADIOTYPE.MOTO))
// cbUserType.Items.Remove(cbUserType.Items[3]);
loadType = usr.UType;
txLogin.Enabled = usr.UType != USERTYPE.Admin;
//cbUserType.Items.Remove(cbUserType.Items[1]); // removes Administrator from dropdown
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
MainForm.needRestart = true;
USERTYPE uType = USERTYPE.Dispatcher;
switch (cbUserType.SelectedIndex)
{
case 0:
uType = USERTYPE.Dispatcher;
break;
case 1:
uType = USERTYPE.Admin;
break;
case 2:
uType = USERTYPE.LiteDispatcher;
break;
case 3:
uType = USERTYPE.TicketingAdmin;
break;
default:
break;
}
if(controlUser.ticketingAdminCount > 0 && uType == USERTYPE.TicketingAdmin)
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("onlyoneticketingadmin"), MainForm.returnLNGString("err"));
return;
}
if ((uType == USERTYPE.Admin) && (!formTypeEdit))
{
if(uType == USERTYPE.Admin) CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("onlyoneadmin"), MainForm.returnLNGString("err"));
}
else
{
if (uType == USERTYPE.Admin && loadType != USERTYPE.Admin)
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("onlyoneadmin"), MainForm.returnLNGString("err"));
return;
}
if (txPass.Text == txConfirm.Text)
{
if (txFirstName.Text != "" && txLasttName.Text != "" && txLogin.Text != "" && txPass.Text != "")
{
if (!formTypeEdit)
{
userResponse resp = DB.addUserToDb(uType, txFirstName.Text, txLasttName.Text, txLogin.Text, txPass.Text, ((User)cbBackupUser.SelectedItem.Value).Id);
//MainForm.CallWebServer(MainForm.RestartSipLink);
if (DBsettings != null && resp == userResponse.done)
{
int userIdx = DB.getUserId(txLogin.Text);
if (userIdx != -1)
{
Console.WriteLine("Adding default settings for user" + userIdx);
DBsettings.AddDefaultSettings(userIdx);
List<GatewayForUser> tmp = DBgateway.getAllGatewaysForNewUser();
foreach (GatewayForUser obj2 in tmp)
DBgateway.addGatewayUserConnection(obj2.Id.ToString(),userIdx.ToString());
}
else
{
Console.WriteLine("Error geting user ID for " + txLogin.Text);
}
}
HandleResp(resp);
}
else
{
userResponse resp = DB.updateUserToDb(uType, txFirstName.Text, txLasttName.Text, txLogin.Text, txPass.Text, editingUser.Id, ((User)cbBackupUser.SelectedItem.Value).Id);
HandleResp(resp);
//MainForm.CallWebServer(MainForm.RestartSipLink);
}
}
else
{
//RadMessageBox.Show("Some fields are empty", "error!",MessageBoxButtons.OK, RadMessageIcon.Error);
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("some"), MainForm.returnLNGString("err"));
}
}
else CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("passmat"), MainForm.returnLNGString("err"));//RadMessageBox.Show("First password does not match with 'Confirm password'.","error!", MessageBoxButtons.OK, RadMessageIcon.Error);
}
}
//udtils
private void HandleResp(userResponse resp)
{
switch (resp)
{
case userResponse.done:
if (!formTypeEdit)
{
CustomMessageBox.Show(CustomMessageBoxType.check, MainForm.returnLNGString("useadd"), MainForm.returnLNGString("done"));
this.Close();
}
else
{
CustomMessageBox.Show(CustomMessageBoxType.check, MainForm.returnLNGString("useedit"), MainForm.returnLNGString("done"));
this.Close();
}
ClearUserInfo();
break;
case userResponse.alreadyInDB:
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("useallin"), MainForm.returnLNGString("err"));
break;
case userResponse.SQLerror:
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("dberr"), MainForm.returnLNGString("err"));
break;
case userResponse.userNotInDB:
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("usein"), MainForm.returnLNGString("err"));
break;
}
}
private void ClearUserInfo()
{
txConfirm.Text = "";
txFirstName.Text = "";
txLasttName.Text = "";
txLogin.Text = "";
txPass.Text = "";
}
private void UsersForm_Load(object sender, EventArgs e)
{
List<User> userList = DB.getAllUsers();
userList.Insert(0,new User(USERTYPE.LiteDispatcher, "", "", "none", "", -1));
cbBackupUser.DataSource = userList.Where(x => x.UserName != txLogin.Text && x.UType != USERTYPE.Admin);
cbBackupUser.DisplayMember = "UserName";
if(editingUser != null)
{
int count = 0;
foreach (User usr in userList)
{
if (usr.Id == editingUser.BackupUserId)
{
cbBackupUser.SelectedIndex = count;
continue;
}
count++;
}
}
lbBackupUser.Visible = cbBackupUser.Visible = (MainForm.radioType == RADIOTYPE.SIMOCO);
}
private void tbSipID_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)Keys.Back)
; // ok
else
e.Handled = true;
}
private void txLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (MainForm.radioType == RADIOTYPE.EXCERA)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)Keys.Back)
; // ok
else
e.Handled = true;
}
}
}
}