Skip to content

Commit

Permalink
Merge branch 'login4L' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gamondue committed Apr 1, 2021
2 parents 25ebe46 + ff98d40 commit ddebdea
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 98 deletions.
56 changes: 24 additions & 32 deletions BusinessLayer/BusinessLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,28 @@ internal class BusinessLayer
DataLayer.DataLayer dl = new DataLayer.DataLayer(); // must be instantiated after reading config file!

#region users' management
internal void CreateUser(User User)
{
dl.CreateUser(User);
}
internal User GetUser(string Username)
{
return dl.GetUser(Username);
}

internal List<User> GetAllUsers()
{
return dl.GetAllUsers();
return dl.GetAllUsers();
}

internal bool UserHasLoginPermission(string Username, string Password)
internal void UpdateUser(User User)
{
// possible filter on user
dl.UpdateUser(User);
}
internal void ChangePassword(User User)
{
dl.ChangePassword(User);
}
internal bool HasUserLoginPermission(string Username, string Password)
{
User uFromDb = GetUser(Username);

Expand All @@ -36,44 +47,25 @@ internal bool UserHasLoginPermission(string Username, string Password)
else
return false;
}
internal bool IsUserAllowed(User CredentialsFromUser)
internal bool IsUserAllowedToLogin(User CredentialsFromUser)
{
User CredentialsFromDatabase = ReadCredentialsFromDatabase(CredentialsFromUser);
User CredentialsFromDatabase = GetUser(CredentialsFromUser.Username);
return (CredentialsFromDatabase.Password == CalculateHash(CredentialsFromUser.Password)
&& CredentialsFromDatabase.Username == CredentialsFromUser.Username);
}

internal void UpdateUser(User User)
{
// possible filter on user
dl.UpdateUser(User);
}

internal void ChangePassword(User User)
{
dl.ChangePassword(User);
}
internal void CreateUser(User User)
{
dl.CreateUser(User);
}
private User ReadCredentialsFromDatabase(User CredentialsFromUser)
{
User u = new User("ugo", "pina");
return u;
}
private User WriteCredentialsToDatabase(User CredentialsFromUser)
{
User u = new User("ugo", "pina");
return u;
}
#endregion
/// <summary>
/// Calculates a SHA256 hash of the string passed and returns it as a ???? base64 ???? string
/// !!!! TODO Function to be done and moved to the Functions class !!!!
/// </summary>
/// <param name="ClearTextPassword"></param>
/// <returns></returns>
private string CalculateHash(string ClearTextPassword)
{
// https://www.mattepuffo.com/blog/articolo/2496-calcolo-hash-sha256-in-csharp.html
SHA256 hash = SHA256.Create();
// !!!! TODO !!!!
return null;
}
#endregion
}
}
47 changes: 23 additions & 24 deletions DataLayer/DataLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,6 @@ public DataLayer()
}
dbName = Commons.PathAndFileDatabase;
}
internal List<User> GetAllUsers()
{
List<User> l = new List<User>();
using (DbConnection conn = Connect())
{
DbCommand cmd = conn.CreateCommand();
string query = "SELECT *" +
" FROM Users";
cmd = new SQLiteCommand(query);
cmd.Connection = conn;
DbDataReader dRead = cmd.ExecuteReader();
while (dRead.Read())
{
User u = GetUserFromRow(dRead);
l.Add(u);
}
dRead.Dispose();
cmd.Dispose();
}
return l;
}

public DataLayer(string PathAndFile)
{
if (!System.IO.File.Exists(PathAndFile))
Expand Down Expand Up @@ -108,7 +86,28 @@ internal User GetUser(string Username)
}
return t;
}
internal User GetUserFromRow(DbDataReader dRead)
internal List<User> GetAllUsers()
{
List<User> l = new List<User>();
using (DbConnection conn = Connect())
{
DbCommand cmd = conn.CreateCommand();
string query = "SELECT *" +
" FROM Users";
cmd = new SQLiteCommand(query);
cmd.Connection = conn;
DbDataReader dRead = cmd.ExecuteReader();
while (dRead.Read())
{
User u = GetUserFromRow(dRead);
l.Add(u);
}
dRead.Dispose();
cmd.Dispose();
}
return l;
}
private User GetUserFromRow(DbDataReader dRead)
{
User u = null;
if (dRead.HasRows)
Expand Down Expand Up @@ -149,7 +148,7 @@ internal void CreateUser(User User)
{
using (DbConnection conn = Connect())
{
// check if username is existing
// check if username is existing. If exists, return null
DbCommand cmd = conn.CreateCommand();
// !!!! TODO !!!!

Expand Down
65 changes: 58 additions & 7 deletions SchoolGrades/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using gamon;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -23,18 +24,68 @@ static void Main()
Commons.ReadConfigFile();
if (!System.IO.File.Exists(Commons.PathAndFileDatabase))
{
MessageBox.Show("Configurazione del programma.\r\nSistemare le cartelle con il percorso dei file (in particalore la cartella che contiene il database), " +
"poi scegliere il file di dati .sqlite e premere 'Salva configurazione'", "SchoolGrades", MessageBoxButtons.OK,MessageBoxIcon.Information);
FrmSetup f = new FrmSetup();
f.ShowDialog();
if (!File.Exists(Commons.PathAndFileDatabase))
// if demo database exists, save the configuration program with demo file
if (Commons.PathAndFileDatabase.Contains("DEMO") && !System.IO.File.Exists(Commons.PathAndFileDatabase))
{
MessageBox.Show("Configurare il programma!", "SchoolGrades", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
WriteConfigFile(); // !!!! TODO move this method to general functions of the program !!!!
}
else
{
MessageBox.Show("Configurazione del programma.\r\nSistemare le cartelle con il percorso dei file (in particalore la cartella che contiene il database), " +
"poi scegliere il file di dati .sqlite e premere 'Salva configurazione'", "SchoolGrades", MessageBoxButtons.OK, MessageBoxIcon.Information);
FrmSetup f = new FrmSetup();
f.ShowDialog();
if (!File.Exists(Commons.PathAndFileDatabase))
{
MessageBox.Show("Configurare il programma!", "SchoolGrades", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
//Application.Run(new frmLogin());
Application.Run(new frmMain());
}
private static void WriteConfigFile()
{
string[] dati = new string[6];
try
{
if (!Directory.Exists(Commons.PathConfig))
Directory.CreateDirectory(Commons.PathConfig);
if (!Directory.Exists(Commons.PathLogs))
Directory.CreateDirectory(Commons.PathLogs);
if (!Directory.Exists(Commons.PathImages))
Directory.CreateDirectory(Commons.PathImages);
if (!Directory.Exists(Commons.PathStartLinks))
Directory.CreateDirectory(Commons.PathStartLinks);
if (!Directory.Exists(Commons.PathDatabase))
Directory.CreateDirectory(Commons.PathDatabase);
if (!Directory.Exists(Commons.PathDocuments))
{
if (Commons.PathDocuments != "")
Directory.CreateDirectory(Commons.PathDocuments);
else
Commons.PathDocuments = ".";
}
dati[0] = Commons.FileDatabase;
dati[1] = Commons.PathImages;
dati[2] = Commons.PathStartLinks;
dati[3] = Commons.PathDatabase;
dati[4] = Commons.PathDocuments;
dati[5] = Commons.SaveBackupWhenExiting.ToString();
#if DEBUG
TextFile.ArrayToFile(Commons.PathAndFileConfig + "_DEBUG", dati, false);
#else
TextFile.ArrayToFile(Commons.PathAndFileConfig, dati, false);
#endif
MessageBox.Show("File di configurazione salvato in " + Commons.PathAndFileConfig);
}
catch (Exception e)
{
Commons.ErrorLog(e.Message, false);
return;
//throw new FileNotFoundException(@"[Error in program's directories] \r\n" + e.Message);
}
}
}
}
2 changes: 1 addition & 1 deletion SchoolGrades/SchoolGrades.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Version>0.51.0.1</Version>
<Version>0.51.0.2</Version>
<Authors>Ing. Gabriele Monti - Forlì - Italia</Authors>
<Copyright>Ing. Gabriele Monti - Forlì - Italia</Copyright>
<PackageLicenseExpression>GPLv2</PackageLicenseExpression>
Expand Down
4 changes: 2 additions & 2 deletions SchoolGrades/bin/Release/netcoreapp3.1/SchoolGrades.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.1": {
"SchoolGrades/0.51.0.1": {
"SchoolGrades/0.51.0.2": {
"dependencies": {
"System.Data.SQLite.Core": "1.0.113.7"
},
Expand Down Expand Up @@ -52,7 +52,7 @@
}
},
"libraries": {
"SchoolGrades/0.51.0.1": {
"SchoolGrades/0.51.0.2": {
"type": "project",
"serviceable": false,
"sha512": ""
Expand Down
Binary file modified SchoolGrades/bin/Release/netcoreapp3.1/SchoolGrades.dll
Binary file not shown.
Binary file modified SchoolGrades/bin/Release/netcoreapp3.1/SchoolGrades.exe
Binary file not shown.
30 changes: 15 additions & 15 deletions SchoolGrades/frmLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ private void frmLogin_Load(object sender, EventArgs e)
db = new DbAndBusiness();
bl = new BusinessLayer.BusinessLayer();

//// test examples
//User u;
//u = new User("pippo", "pluto");
////u = new User("pina", "pluto");
//////u = new User("ugo", "pina");
////bl.CreateUser(u);
//u.Password = "mariangela";
//bl.ChangePassword(u);
// test examples
User u;
u = new User("pippo", "pluto");
//u = new User("pina", "pluto");
////u = new User("ugo", "pina");
//bl.CreateUser(u);
u.Password = "mariangela";
bl.ChangePassword(u);

//u.FirstName = "Ugo";
//u.LastName = "Fantozzi";
//u.Email = "u.fantozzi@megaditta.com";
//u.Description = "Inferiore Rag. Ugo Fantozzi";
//bl.UpdateUser(u);
u.FirstName = "Ugo";
u.LastName = "Fantozzi";
u.Email = "u.fantozzi@megaditta.com";
u.Description = "Inferiore Rag. Ugo Fantozzi";
bl.UpdateUser(u);

//User u1 = bl.GetUser("ugo");
User u1 = bl.GetUser("ugo");
}
private void btnOk_Click(object sender, EventArgs e)
{
if (bl.UserHasLoginPermission(txtUsername.Text,
if (bl.HasUserLoginPermission(txtUsername.Text,
txtPassword.Text))
{
frmMain f = new frmMain();
Expand Down
12 changes: 6 additions & 6 deletions SchoolGrades/frmQuestionChoose.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions SchoolGrades/frmSetup.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ddebdea

Please sign in to comment.