using System;
using System.Data;
using System.Configuration;
using System.Collections;
using umbraco.DataLayer;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace umbraco.BusinessLogic
{
///
/// represents a Umbraco back end user
///
public class User
{
private int _id;
private bool _isInitialized;
private string _name;
private string _loginname;
private int _startnodeid;
private int _startmediaid;
private string _email;
private string _language = "";
private UserType _usertype;
private bool _userNoConsole;
private bool _userDisabled;
private bool _defaultToLiveEditing;
private Hashtable _cruds = new Hashtable();
private bool _crudsInitialized = false;
private Hashtable _notifications = new Hashtable();
private bool _notificationsInitialized = false;
private static ISqlHelper SqlHelper
{
get { return Application.SqlHelper; }
}
///
/// Initializes a new instance of the class.
///
/// The ID.
public User(int ID)
{
setupUser(ID);
}
///
/// Initializes a new instance of the class.
///
/// The ID.
/// if set to true [no setup].
public User(int ID, bool noSetup)
{
_id = ID;
}
///
/// Initializes a new instance of the class.
///
/// The login.
/// The password.
public User(string Login, string Password)
{
setupUser(getUserId(Login, Password));
}
///
/// Initializes a new instance of the class.
///
/// The login.
public User(string Login)
{
setupUser(getUserId(Login));
}
private void setupUser(int ID)
{
_id = ID;
using (IRecordsReader dr = SqlHelper.ExecuteReader(
"Select userNoConsole, userDisabled, userType,startStructureID, startMediaId, userName,userLogin,userEmail,userDefaultPermissions, userLanguage, defaultToLiveEditing from umbracoUser where id = @id",
SqlHelper.CreateParameter("@id", ID)))
{
if (dr.Read())
{
_userNoConsole = dr.GetBoolean("usernoconsole");
_userDisabled = dr.GetBoolean("userDisabled");
_name = dr.GetString("userName");
_loginname = dr.GetString("userLogin");
_email = dr.GetString("userEmail");
_language = dr.GetString("userLanguage");
_startnodeid = dr.GetInt("startStructureID");
if (!dr.IsNull("startMediaId"))
_startmediaid = dr.GetInt("startMediaID");
_usertype = UserType.GetUserType(dr.GetShort("UserType"));
_defaultToLiveEditing = dr.GetBoolean("defaultToLiveEditing");
}
else
{
throw new ArgumentException("No User exists with ID " + ID.ToString());
}
}
_isInitialized = true;
}
///
/// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility
///
public void Save()
{
OnSaving(EventArgs.Empty);
}
///
/// Gets or sets the users name.
///
/// The name.
public string Name
{
get
{
if (!_isInitialized)
setupUser(_id);
return _name;
}
set
{
_name = value;
SqlHelper.ExecuteNonQuery("Update umbracoUser set UserName = @userName where id = @id", SqlHelper.CreateParameter("@userName", value), SqlHelper.CreateParameter("@id", Id));
FlushFromCache();
}
}
///
/// Gets or sets the users email.
///
/// The email.
public string Email
{
get
{
if (!_isInitialized)
setupUser(_id);
return _email;
}
set
{
_email = value;
SqlHelper.ExecuteNonQuery("Update umbracoUser set UserEmail = @email where id = @id", SqlHelper.CreateParameter("@id", this.Id), SqlHelper.CreateParameter("@email", value));
FlushFromCache();
}
}
///
/// Gets or sets the users language.
///
/// The language.
public string Language
{
get
{
if (!_isInitialized)
setupUser(_id);
return _language;
}
set
{
_language = value;
SqlHelper.ExecuteNonQuery("Update umbracoUser set userLanguage = @language where id = @id", SqlHelper.CreateParameter("@language", value), SqlHelper.CreateParameter("@id", Id));
FlushFromCache();
}
}
///
/// Gets or sets the users password.
///
/// The password.
public string Password
{
get
{
return GetPassword();
}
set
{
SqlHelper.ExecuteNonQuery("Update umbracoUser set UserPassword = @pw where id = @id", SqlHelper.CreateParameter("@pw", value), SqlHelper.CreateParameter("@id", Id));
FlushFromCache();
}
}
///
/// Gets the password.
///
///
public string GetPassword()
{
return
SqlHelper.ExecuteScalar("select UserPassword from umbracoUser where id = @id",
SqlHelper.CreateParameter("@id", this.Id));
}
static string _connstring = GlobalSettings.DbDSN;
///
/// Determines whether this user is an admin.
///
///
/// true if this user is admin; otherwise, false.
///
public bool IsAdmin()
{
return UserType.Alias == "admin";
}
public bool ValidatePassword(string password)
{
string userLogin =
SqlHelper.ExecuteScalar("select userLogin from umbracoUser where userLogin = @login and UserPassword = @pw",
SqlHelper.CreateParameter("@pw", password),
SqlHelper.CreateParameter("@login", LoginName)
);
return userLogin == this.LoginName;
}
///
/// Determines whether this user is the root (super user).
///
///
/// true if this user is root; otherwise, false.
///
public bool IsRoot()
{
return Id == 0;
}
///
/// Gets the applications which the user has access to.
///
/// The users applications.
public Application[] Applications
{
get
{
if (!_isInitialized)
setupUser(_id);
var apps = new List();
using (IRecordsReader appIcons = SqlHelper.ExecuteReader("select appAlias, appIcon, appname from umbracoApp app join umbracoUser2app u2a on u2a.app = app.appAlias and u2a.[user] = @userID order by app.sortOrder", SqlHelper.CreateParameter("@userID", this.Id)))
{
while (appIcons.Read())
{
Application tmp = new Application();
tmp.name = appIcons.GetString("appName");
tmp.icon = appIcons.GetString("appIcon");
tmp.alias = appIcons.GetString("appAlias");
apps.Add(tmp);
}
}
return apps.ToArray();
}
}
///
/// Gets or sets the users login name
///
/// The loginname.
public string LoginName
{
get
{
if (!_isInitialized)
setupUser(_id);
return _loginname;
}
set
{
if (!ensureUniqueLoginName(value, this))
throw new Exception(String.Format("A user with the login '{0}' already exists", value));
_loginname = value;
SqlHelper.ExecuteNonQuery("Update umbracoUser set UserLogin = @login where id = @id", SqlHelper.CreateParameter("@login", value), SqlHelper.CreateParameter("@id", Id));
FlushFromCache();
}
}
private static bool ensureUniqueLoginName(string loginName, User currentUser)
{
User[] u = User.getAllByLoginName(loginName);
if (u.Length != 0)
{
if (u[0].Id != currentUser.Id)
return false;
}
return true;
}
///
/// Validates the users credentials.
///
/// The login name.
/// The password.
///
public static bool validateCredentials(string lname, string passw)
{
return validateCredentials(lname, passw, true);
}
///
/// Validates the users credentials.
///
/// The login name.
/// The password.
/// if set to true [check for umbraco console access].
///
public static bool validateCredentials(string lname, string passw, bool checkForUmbracoConsoleAccess)
{
string consoleCheckSql = "";
if (checkForUmbracoConsoleAccess)
consoleCheckSql = "and userNoConsole = 0 ";
object tmp = SqlHelper.ExecuteScalar