Rename to VAR.Focus
This commit is contained in:
146
VAR.Focus.Web/Code/BusinessLogic/Users.cs
Normal file
146
VAR.Focus.Web/Code/BusinessLogic/Users.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using VAR.Focus.Web.Code.Entities;
|
||||
using VAR.Focus.Web.Code.JSON;
|
||||
|
||||
namespace VAR.Focus.Web.Code.BusinessLogic
|
||||
{
|
||||
public class Users
|
||||
{
|
||||
#region declarations
|
||||
|
||||
private static Users _currentInstance = null;
|
||||
|
||||
private List<User> _users = new List<User>();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public static Users Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_currentInstance == null)
|
||||
{
|
||||
_currentInstance = new Users();
|
||||
}
|
||||
return _currentInstance;
|
||||
}
|
||||
set { _currentInstance = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Life cycle
|
||||
|
||||
public Users()
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public methods
|
||||
|
||||
public User User_GetByName(string name)
|
||||
{
|
||||
name=name.ToLower();
|
||||
foreach (User userAux in _users)
|
||||
{
|
||||
if (name.CompareTo(userAux.Name.ToLower()) == 0)
|
||||
{
|
||||
return userAux;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public User User_GetByEmail(string email)
|
||||
{
|
||||
email = email.ToLower();
|
||||
foreach (User userAux in _users)
|
||||
{
|
||||
if (email.CompareTo(userAux.Email.ToLower()) == 0)
|
||||
{
|
||||
return userAux;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public User User_GetByNameOrEmail(string name, string email)
|
||||
{
|
||||
name = name.ToLower();
|
||||
email = email.ToLower();
|
||||
foreach (User userAux in _users)
|
||||
{
|
||||
if (name.CompareTo(userAux.Name.ToLower()) == 0 ||
|
||||
email.CompareTo(userAux.Email.ToLower()) == 0)
|
||||
{
|
||||
return userAux;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public User User_Set(string name, string email, string password)
|
||||
{
|
||||
User user = null;
|
||||
bool isNew = false;
|
||||
lock (_users)
|
||||
{
|
||||
user = User_GetByName(name);
|
||||
if (user == null) { user = User_GetByEmail(name); }
|
||||
if (user == null) { user = new User(); isNew = true; }
|
||||
|
||||
user.Name = name;
|
||||
user.Email = email;
|
||||
if (string.IsNullOrEmpty(password) == false)
|
||||
{
|
||||
user.PasswordSalt = CryptoUtils.GetCryptoToken();
|
||||
user.PasswordHash = CryptoUtils.GetHashedPassword(password, user.PasswordSalt);
|
||||
}
|
||||
|
||||
if (isNew) { _users.Add(user); }
|
||||
|
||||
SaveData();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public bool User_Authenticate(string nameOrMail, string password)
|
||||
{
|
||||
User user = User_GetByNameOrEmail(nameOrMail, nameOrMail);
|
||||
if (user == null) { return false; }
|
||||
|
||||
string passwordHash = CryptoUtils.GetHashedPassword(password, user.PasswordSalt);
|
||||
if (passwordHash != user.PasswordHash) { return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
#region Persistence
|
||||
|
||||
private const string PersistenceFile = "priv/users.json";
|
||||
|
||||
private void LoadData()
|
||||
{
|
||||
_users = Persistence.LoadList<User>(PersistenceFile);
|
||||
}
|
||||
|
||||
private void SaveData()
|
||||
{
|
||||
Persistence.SaveList(PersistenceFile, _users);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user