Skip to content

Commit

Permalink
refactor & overwrite existing admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
romanett committed Dec 28, 2023
1 parent 94607b3 commit c837abe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 46 deletions.
60 changes: 35 additions & 25 deletions Samples/GDS/ConsoleServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using Mono.Options;
using Opc.Ua.Configuration;
using Opc.Ua.Gds.Server.Database;
using Opc.Ua.Gds.Server.Database.Linq;
using Opc.Ua.Server;
using System;
Expand Down Expand Up @@ -243,33 +244,9 @@ private async Task ConsoleGlobalDiscoveryServer()
var database = JsonApplicationsDatabase.Load(databaseStorePath);
var userDatabase = JsonUsersDatabase.Load(userdatabaseStorePath);


//configure Users
ApplicationInstance.MessageDlg.Message("Use default users?", true);
bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result;

if (!createStandardUsers){
//delete existing standard users
userDatabase.DeleteUser("appadmin");
userDatabase.DeleteUser("appuser");
userDatabase.DeleteUser("sysadmin");

//Create new admin user
Console.Write("Please specify user name of the application admin user:");
string username = Console.ReadLine();
_ = username ?? throw new ArgumentNullException("User name is not allowed to be empty");

Console.Write($"Please specify the password of {username}:");

//string password = Console.ReadLine();
string password = GetPassword();
_ = password ?? throw new ArgumentNullException("Password is not allowed to be empty");

userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin);
}
bool createStandardUsers = ConfigureUsers(userDatabase);

// start the server.

server = new GlobalDiscoverySampleServer(
database,
database,
Expand All @@ -296,6 +273,39 @@ private async Task ConsoleGlobalDiscoveryServer()

}

private bool ConfigureUsers(JsonUsersDatabase userDatabase)
{
ApplicationInstance.MessageDlg.Message("Use default users?", true);
bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result;

if (!createStandardUsers)
{
//delete existing standard users
userDatabase.DeleteUser("appadmin");
userDatabase.DeleteUser("appuser");
userDatabase.DeleteUser("sysadmin");

//Create new admin user
Console.Write("Please specify user name of the application admin user:");
string username = Console.ReadLine();
_ = username ?? throw new ArgumentNullException("User name is not allowed to be empty");

Console.Write($"Please specify the password of {username}:");

//string password = Console.ReadLine();
string password = GetPassword();
_ = password ?? throw new ArgumentNullException("Password is not allowed to be empty");

//create User, if User exists delete & recreate
if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin))
{
userDatabase.DeleteUser(username);
userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin);
}
}
return createStandardUsers;
}

private void EventStatus(Session session, SessionEventReason reason)
{
lastEventTime = DateTime.UtcNow;
Expand Down
53 changes: 32 additions & 21 deletions Samples/GDS/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,8 @@ static void Main()
//initialize users Database
userDatabase.Initialize();

//configure users
ApplicationInstance.MessageDlg.Message("Use default users?", true);
bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result;
if (!createStandardUsers)
{
//Delete existing standard users
userDatabase.DeleteUser("appadmin");
userDatabase.DeleteUser("appuser");
userDatabase.DeleteUser("sysadmin");

//Create new admin user
string username = InputDlg.Show("Please specify user name of the application admin user:", false);
_ = username ?? throw new ArgumentNullException("User name is not allowed to be empty");

Console.Write($"Please specify the password of {username}:");

string password = InputDlg.Show($"Please specify the password of {username}:", true);
_ = password ?? throw new ArgumentNullException("Password is not allowed to be empty");

userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin);
}
bool createStandardUsers = ConfigureUsers(userDatabase);


// start the server.
var database = new SqlApplicationsDatabase();
Expand All @@ -115,5 +96,35 @@ static void Main()
ExceptionDlg.Show(application.ApplicationName, e);
}
}

private static bool ConfigureUsers(SqlUsersDatabase userDatabase)
{
ApplicationInstance.MessageDlg.Message("Use default users?", true);
bool createStandardUsers = ApplicationInstance.MessageDlg.ShowAsync().Result;
if (!createStandardUsers)
{
//Delete existing standard users
userDatabase.DeleteUser("appadmin");
userDatabase.DeleteUser("appuser");
userDatabase.DeleteUser("sysadmin");

//Create new admin user
string username = InputDlg.Show("Please specify user name of the application admin user:", false);
_ = username ?? throw new ArgumentNullException("User name is not allowed to be empty");

Console.Write($"Please specify the password of {username}:");

string password = InputDlg.Show($"Please specify the password of {username}:", true);
_ = password ?? throw new ArgumentNullException("Password is not allowed to be empty");

//create User, if User exists delete & recreate
if (!userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin))
{
userDatabase.DeleteUser(username);
userDatabase.CreateUser(username, password, GdsRole.ApplicationAdmin);
}
}
return createStandardUsers;
}
}
}

0 comments on commit c837abe

Please sign in to comment.