2009-06-19 07:39:16 +00:00
using System ;
using System.Data ;
using System.Configuration ;
using System.Collections ;
using umbraco.DataLayer ;
2010-04-08 15:09:59 +00:00
using System.Collections.Generic ;
using System.Linq ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
using System.Runtime.CompilerServices ;
2009-06-19 07:39:16 +00:00
2010-06-10 22:13:34 +00:00
namespace umbraco.BusinessLogic
{
2009-06-19 07:39:16 +00:00
/// <summary>
/// represents a Umbraco back end user
/// </summary>
2010-06-10 22:13:34 +00:00
public class User
{
2009-06-19 07:39:16 +00:00
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 ;
2010-06-10 22:13:34 +00:00
private static ISqlHelper SqlHelper
{
2009-06-19 07:39:16 +00:00
get { return Application . SqlHelper ; }
}
/// <summary>
/// Initializes a new instance of the <see cref="User"/> class.
/// </summary>
/// <param name="ID">The ID.</param>
2010-06-10 22:13:34 +00:00
public User ( int ID )
{
2009-06-19 07:39:16 +00:00
setupUser ( ID ) ;
}
/// <summary>
/// Initializes a new instance of the <see cref="User"/> class.
/// </summary>
/// <param name="ID">The ID.</param>
/// <param name="noSetup">if set to <c>true</c> [no setup].</param>
2010-06-10 22:13:34 +00:00
public User ( int ID , bool noSetup )
{
2009-06-19 07:39:16 +00:00
_id = ID ;
}
/// <summary>
/// Initializes a new instance of the <see cref="User"/> class.
/// </summary>
/// <param name="Login">The login.</param>
/// <param name="Password">The password.</param>
2010-06-10 22:13:34 +00:00
public User ( string Login , string Password )
{
2009-06-19 07:39:16 +00:00
setupUser ( getUserId ( Login , Password ) ) ;
}
/// <summary>
/// Initializes a new instance of the <see cref="User"/> class.
/// </summary>
/// <param name="Login">The login.</param>
2010-06-10 22:13:34 +00:00
public User ( string Login )
{
2009-06-19 07:39:16 +00:00
setupUser ( getUserId ( Login ) ) ;
}
2010-06-10 22:13:34 +00:00
private void setupUser ( int ID )
{
2009-06-19 07:39:16 +00:00
_id = ID ;
using ( IRecordsReader dr = SqlHelper . ExecuteReader (
"Select userNoConsole, userDisabled, userType,startStructureID, startMediaId, userName,userLogin,userEmail,userDefaultPermissions, userLanguage, defaultToLiveEditing from umbracoUser where id = @id" ,
2010-06-10 22:13:34 +00:00
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" ) ;
}
2010-06-11 13:06:35 +00:00
else
{
throw new ArgumentException ( "No User exists with ID " + ID . ToString ( ) ) ;
}
2009-06-19 07:39:16 +00:00
}
_isInitialized = true ;
}
/// <summary>
/// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility
/// </summary>
2010-06-10 22:13:34 +00:00
public void Save ( )
{
2009-06-19 07:39:16 +00:00
OnSaving ( EventArgs . Empty ) ;
}
/// <summary>
/// Gets or sets the users name.
/// </summary>
/// <value>The name.</value>
2010-06-10 22:13:34 +00:00
public string Name
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _name ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_name = value ;
SqlHelper . ExecuteNonQuery ( "Update umbracoUser set UserName = @userName where id = @id" , SqlHelper . CreateParameter ( "@userName" , value ) , SqlHelper . CreateParameter ( "@id" , Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets the users email.
/// </summary>
/// <value>The email.</value>
2010-06-10 22:13:34 +00:00
public string Email
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _email ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_email = value ;
SqlHelper . ExecuteNonQuery ( "Update umbracoUser set UserEmail = @email where id = @id" , SqlHelper . CreateParameter ( "@id" , this . Id ) , SqlHelper . CreateParameter ( "@email" , value ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets the users language.
/// </summary>
/// <value>The language.</value>
2010-06-10 22:13:34 +00:00
public string Language
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _language ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_language = value ;
SqlHelper . ExecuteNonQuery ( "Update umbracoUser set userLanguage = @language where id = @id" , SqlHelper . CreateParameter ( "@language" , value ) , SqlHelper . CreateParameter ( "@id" , Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets the users password.
/// </summary>
/// <value>The password.</value>
2010-06-10 22:13:34 +00:00
public string Password
{
get
{
2009-06-19 07:39:16 +00:00
return GetPassword ( ) ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( "Update umbracoUser set UserPassword = @pw where id = @id" , SqlHelper . CreateParameter ( "@pw" , value ) , SqlHelper . CreateParameter ( "@id" , Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets the password.
/// </summary>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public string GetPassword ( )
{
return
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteScalar < string > ( "select UserPassword from umbracoUser where id = @id" ,
SqlHelper . CreateParameter ( "@id" , this . Id ) ) ;
}
static string _connstring = GlobalSettings . DbDSN ;
/// <summary>
/// Determines whether this user is an admin.
/// </summary>
/// <returns>
/// <c>true</c> if this user is admin; otherwise, <c>false</c>.
/// </returns>
2010-06-10 22:13:34 +00:00
public bool IsAdmin ( )
{
2009-06-19 07:39:16 +00:00
return UserType . Alias = = "admin" ;
}
2010-06-10 22:13:34 +00:00
public bool ValidatePassword ( string password )
{
2009-06-19 07:39:16 +00:00
string userLogin =
2010-06-10 22:13:34 +00:00
SqlHelper . ExecuteScalar < string > ( "select userLogin from umbracoUser where userLogin = @login and UserPassword = @pw" ,
2009-06-19 07:39:16 +00:00
SqlHelper . CreateParameter ( "@pw" , password ) ,
SqlHelper . CreateParameter ( "@login" , LoginName )
) ;
return userLogin = = this . LoginName ;
}
/// <summary>
/// Determines whether this user is the root (super user).
/// </summary>
/// <returns>
/// <c>true</c> if this user is root; otherwise, <c>false</c>.
/// </returns>
2010-06-10 22:13:34 +00:00
public bool IsRoot ( )
{
2009-06-19 07:39:16 +00:00
return Id = = 0 ;
}
/// <summary>
/// Gets the applications which the user has access to.
/// </summary>
/// <value>The users applications.</value>
2010-06-10 22:13:34 +00:00
public Application [ ] Applications
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
var apps = new List < Application > ( ) ;
2009-06-19 07:39:16 +00:00
2010-06-10 22:13:34 +00:00
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 ( ) )
{
2009-06-19 07:39:16 +00:00
Application tmp = new Application ( ) ;
tmp . name = appIcons . GetString ( "appName" ) ;
tmp . icon = appIcons . GetString ( "appIcon" ) ;
tmp . alias = appIcons . GetString ( "appAlias" ) ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
apps . Add ( tmp ) ;
2009-06-19 07:39:16 +00:00
}
}
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
return apps . ToArray ( ) ;
2009-06-19 07:39:16 +00:00
}
}
/// <summary>
/// Gets or sets the users login name
/// </summary>
/// <value>The loginname.</value>
2010-06-10 22:13:34 +00:00
public string LoginName
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _loginname ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
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 ( ) ;
}
}
2010-06-10 22:13:34 +00:00
private static bool ensureUniqueLoginName ( string loginName , User currentUser )
{
2009-06-19 07:39:16 +00:00
User [ ] u = User . getAllByLoginName ( loginName ) ;
if ( u . Length ! = 0 )
{
if ( u [ 0 ] . Id ! = currentUser . Id )
return false ;
}
return true ;
}
/// <summary>
/// Validates the users credentials.
/// </summary>
/// <param name="lname">The login name.</param>
/// <param name="passw">The password.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static bool validateCredentials ( string lname , string passw )
{
2009-06-19 07:39:16 +00:00
return validateCredentials ( lname , passw , true ) ;
}
/// <summary>
/// Validates the users credentials.
/// </summary>
/// <param name="lname">The login name.</param>
/// <param name="passw">The password.</param>
/// <param name="checkForUmbracoConsoleAccess">if set to <c>true</c> [check for umbraco console access].</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static bool validateCredentials ( string lname , string passw , bool checkForUmbracoConsoleAccess )
{
2009-06-19 07:39:16 +00:00
string consoleCheckSql = "" ;
if ( checkForUmbracoConsoleAccess )
consoleCheckSql = "and userNoConsole = 0 " ;
object tmp = SqlHelper . ExecuteScalar < object > (
"select id from umbracoUser where userDisabled = 0 " + consoleCheckSql + " and userLogin = @login and userPassword = @pw" , SqlHelper . CreateParameter ( "@login" , lname ) , SqlHelper . CreateParameter ( "@pw" , passw )
) ;
// Logging
if ( tmp = = null )
BusinessLogic . Log . Add ( BusinessLogic . LogTypes . LoginFailure , BusinessLogic . User . GetUser ( 0 ) , - 1 , "Login: '" + lname + "' failed, from IP: " + System . Web . HttpContext . Current . Request . UserHostAddress ) ;
return ( tmp ! = null ) ;
}
/// <summary>
/// Gets or sets the type of the user.
/// </summary>
/// <value>The type of the user.</value>
2010-06-10 22:13:34 +00:00
public UserType UserType
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _usertype ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_usertype = value ;
SqlHelper . ExecuteNonQuery (
@"Update umbracoUser set userType = @type where id = @id" ,
SqlHelper . CreateParameter ( "@type" , value . Id ) ,
SqlHelper . CreateParameter ( "@id" , Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets all users
/// </summary>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static User [ ] getAll ( )
{
2009-06-19 07:39:16 +00:00
IRecordsReader dr ;
dr = SqlHelper . ExecuteReader ( "Select id from umbracoUser" ) ;
2010-04-08 15:09:59 +00:00
List < User > users = new List < User > ( ) ;
2010-06-10 22:13:34 +00:00
while ( dr . Read ( ) )
2010-04-08 15:09:59 +00:00
{
users . Add ( User . GetUser ( dr . GetInt ( "id" ) ) ) ;
2009-06-19 07:39:16 +00:00
}
dr . Close ( ) ;
2010-04-08 15:09:59 +00:00
return users . OrderBy ( x = > x . Name ) . ToArray ( ) ;
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Gets the current user (logged in)
/// </summary>
/// <returns>A user or null</returns>
public static User GetCurrent ( )
{
if ( umbraco . BasePages . BasePage . umbracoUserContextID ! = "" )
return BusinessLogic . User . GetUser ( umbraco . BasePages . BasePage . GetUserId ( umbraco . BasePages . BasePage . umbracoUserContextID ) ) ;
else
return null ;
}
/// <summary>
/// Gets all users by email.
/// </summary>
/// <param name="email">The email.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static User [ ] getAllByEmail ( string email )
{
2010-07-20 09:47:27 +00:00
List < User > retVal = new List < User > ( ) ;
2009-06-19 07:39:16 +00:00
System . Collections . ArrayList tmpContainer = new System . Collections . ArrayList ( ) ;
IRecordsReader dr ;
dr = SqlHelper . ExecuteReader (
2010-07-20 09:47:27 +00:00
"Select id from umbracoUser where userEmail LIKE @email" , SqlHelper . CreateParameter ( "@email" , String . Format ( "%{0}%" , email ) ) ) ;
2009-06-19 07:39:16 +00:00
2010-06-10 22:13:34 +00:00
while ( dr . Read ( ) )
{
2010-07-20 09:47:27 +00:00
retVal . Add ( BusinessLogic . User . GetUser ( dr . GetInt ( "id" ) ) ) ;
2009-06-19 07:39:16 +00:00
}
dr . Close ( ) ;
2010-07-20 09:47:27 +00:00
return retVal . ToArray ( ) ;
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Gets all users by login name.
/// </summary>
/// <param name="login">The login.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static User [ ] getAllByLoginName ( string login )
{
2010-06-21 19:10:33 +00:00
return GetAllByLoginName ( login , false ) . ToArray ( ) ;
}
2009-06-19 07:39:16 +00:00
2010-06-21 19:10:33 +00:00
public static IEnumerable < User > GetAllByLoginName ( string login , bool partialMatch )
{
2009-06-19 07:39:16 +00:00
2010-06-21 19:10:33 +00:00
var users = new List < User > ( ) ;
2009-06-19 07:39:16 +00:00
2010-06-21 19:10:33 +00:00
if ( partialMatch )
2010-07-20 09:47:27 +00:00
{
2010-06-21 19:10:33 +00:00
using ( var dr = SqlHelper . ExecuteReader (
"Select id from umbracoUser where userLogin LIKE @login" , SqlHelper . CreateParameter ( "@login" , String . Format ( "%{0}%" , login ) ) ) )
{
while ( dr . Read ( ) )
{
users . Add ( BusinessLogic . User . GetUser ( dr . GetInt ( "id" ) ) ) ;
}
}
}
else
2010-06-10 22:13:34 +00:00
{
2010-06-21 19:10:33 +00:00
using ( var dr = SqlHelper . ExecuteReader (
"Select id from umbracoUser where userLogin=@login" , SqlHelper . CreateParameter ( "@login" , login ) ) )
{
while ( dr . Read ( ) )
{
users . Add ( BusinessLogic . User . GetUser ( dr . GetInt ( "id" ) ) ) ;
}
}
2009-06-19 07:39:16 +00:00
}
2010-06-21 19:10:33 +00:00
return users ;
2010-07-20 09:47:27 +00:00
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Create a new user.
/// </summary>
/// <param name="name">The full name.</param>
/// <param name="lname">The login name.</param>
/// <param name="passw">The password.</param>
/// <param name="ut">The user type.</param>
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
[MethodImpl(MethodImplOptions.Synchronized)]
public static User MakeNew ( string name , string lname , string passw , UserType ut )
2010-06-10 22:13:34 +00:00
{
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( @ "
insert into umbracoUser
( UserType , startStructureId , startMediaId , UserName , userLogin , userPassword , userEmail , userLanguage )
values ( @type , - 1 , - 1 , @name , @lname , @pw , ' ' , @lang ) ",
SqlHelper . CreateParameter ( "@lang" , GlobalSettings . DefaultUILanguage ) ,
SqlHelper . CreateParameter ( "@name" , name ) ,
SqlHelper . CreateParameter ( "@lname" , lname ) ,
SqlHelper . CreateParameter ( "@type" , ut . Id ) ,
SqlHelper . CreateParameter ( "@pw" , passw ) ) ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
var u = new User ( lname ) ;
u . OnNew ( EventArgs . Empty ) ;
return u ;
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Creates a new user.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="lname">The lname.</param>
/// <param name="passw">The passw.</param>
/// <param name="email">The email.</param>
/// <param name="ut">The ut.</param>
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
[MethodImpl(MethodImplOptions.Synchronized)]
public static User MakeNew ( string name , string lname , string passw , string email , UserType ut )
2010-06-10 22:13:34 +00:00
{
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( @ "
insert into umbracoUser
( UserType , startStructureId , startMediaId , UserName , userLogin , userPassword , userEmail , userLanguage )
values ( @type , - 1 , - 1 , @name , @lname , @pw , @email , @lang ) ",
SqlHelper . CreateParameter ( "@lang" , GlobalSettings . DefaultUILanguage ) ,
SqlHelper . CreateParameter ( "@name" , name ) ,
SqlHelper . CreateParameter ( "@lname" , lname ) ,
SqlHelper . CreateParameter ( "@email" , email ) ,
SqlHelper . CreateParameter ( "@type" , ut . Id ) ,
SqlHelper . CreateParameter ( "@pw" , passw ) ) ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
var u = new User ( lname ) ;
u . OnNew ( EventArgs . Empty ) ;
return u ;
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Updates the name, login name and password for the user with the specified id.
/// </summary>
/// <param name="id">The id.</param>
/// <param name="name">The name.</param>
/// <param name="lname">The lname.</param>
/// <param name="email">The email.</param>
/// <param name="ut">The ut.</param>
2010-06-10 22:13:34 +00:00
public static void Update ( int id , string name , string lname , string email , UserType ut )
{
2009-06-19 07:39:16 +00:00
if ( ! ensureUniqueLoginName ( lname , User . GetUser ( id ) ) )
throw new Exception ( String . Format ( "A user with the login '{0}' already exists" , lname ) ) ;
2010-06-10 22:13:34 +00:00
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( @"Update umbracoUser set userName=@name, userLogin=@lname, userEmail=@email, UserType=@type where id = @id" ,
SqlHelper . CreateParameter ( "@name" , name ) ,
SqlHelper . CreateParameter ( "@lname" , lname ) ,
SqlHelper . CreateParameter ( "@email" , email ) ,
SqlHelper . CreateParameter ( "@type" , ut . Id ) ,
SqlHelper . CreateParameter ( "@id" , id ) ) ;
}
/// <summary>
/// Gets the ID from the user with the specified login name and password
/// </summary>
/// <param name="lname">The login name.</param>
/// <param name="passw">The password.</param>
/// <returns>a user ID</returns>
2010-06-10 22:13:34 +00:00
public static int getUserId ( string lname , string passw )
{
2009-06-19 07:39:16 +00:00
return getUserId ( "select id from umbracoUser where userDisabled = 0 and userNoConsole = 0 and userLogin = @login and userPassword = @pw" ,
SqlHelper . CreateParameter ( "@login" , lname ) ,
SqlHelper . CreateParameter ( "@pw" , passw ) ) ;
}
/// <summary>
/// Gets the ID from the user with the specified login name
/// </summary>
/// <param name="lname">The login name.</param>
/// <returns>a user ID</returns>
2010-06-10 22:13:34 +00:00
public static int getUserId ( string lname )
{
2009-06-19 07:39:16 +00:00
return getUserId ( "select id from umbracoUser where userLogin = @login" ,
SqlHelper . CreateParameter ( "@login" , lname ) ) ;
}
2010-06-10 22:13:34 +00:00
private static int getUserId ( string query , params IParameter [ ] parameterValues )
{
2009-06-19 07:39:16 +00:00
object userId = SqlHelper . ExecuteScalar < object > ( query , parameterValues ) ;
2010-06-10 22:13:34 +00:00
return ( userId ! = null & & userId ! = DBNull . Value ) ? int . Parse ( userId . ToString ( ) ) : - 1 ;
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Deletes this instance.
/// </summary>
2010-06-10 19:43:27 +00:00
[Obsolete("Deleting users are NOT supported as history needs to be kept. Please use the disable() method instead")]
2010-06-10 22:13:34 +00:00
public void delete ( )
{
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
//make sure you cannot delete the admin user!
if ( this . Id = = 0 )
throw new InvalidOperationException ( "The Administrator account cannot be deleted" ) ;
2009-06-19 07:39:16 +00:00
OnDeleting ( EventArgs . Empty ) ;
More unit tests and sql cleanup scripts.
Fixes: 27516, 27517, 27518, 27519, 27520, 27521, 27525, 27526, 27527, 27528, 27529
[TFS Changeset #68129]
2010-06-12 15:01:24 +00:00
//would be better in the notifications class but since we can't reference the cms project (poorly architected) we need to use raw sql
SqlHelper . ExecuteNonQuery ( "delete from umbracoUser2NodeNotify where userId = @userId" , SqlHelper . CreateParameter ( "@userId" , Id ) ) ;
//would be better in the permissions class but since we can't reference the cms project (poorly architected) we need to use raw sql
SqlHelper . ExecuteNonQuery ( "delete from umbracoUser2NodePermission where userId = @userId" , SqlHelper . CreateParameter ( "@userId" , Id ) ) ;
//delete the assigned applications
clearApplications ( ) ;
2010-06-10 08:26:34 +00:00
SqlHelper . ExecuteNonQuery ( "delete from umbracoUserLogins where userID = @id" , SqlHelper . CreateParameter ( "@id" , Id ) ) ;
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( "delete from umbracoUser where id = @id" , SqlHelper . CreateParameter ( "@id" , Id ) ) ;
FlushFromCache ( ) ;
}
/// <summary>
/// Disables this instance.
/// </summary>
2010-06-10 22:13:34 +00:00
public void disable ( )
{
2009-06-19 07:39:16 +00:00
OnDisabling ( EventArgs . Empty ) ;
2010-06-21 15:09:34 +00:00
//change disabled and userLogin (prefix with yyyyMMdd_ )
2009-06-19 07:39:16 +00:00
this . Disabled = true ;
2010-06-21 15:09:34 +00:00
//can't rename if it's going to take up too many chars
if ( this . LoginName . Length + 9 < = 125 )
{
this . LoginName = DateTime . Now . ToString ( "yyyyMMdd" ) + "_" + this . LoginName ;
}
2010-07-20 09:47:27 +00:00
2009-06-19 07:39:16 +00:00
}
/// <summary>
/// Gets the users permissions based on a nodes path
/// </summary>
/// <param name="Path">The path.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public string GetPermissions ( string Path )
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
string cruds = UserType . DefaultPermissions ;
if ( ! _crudsInitialized )
initCruds ( ) ;
2010-06-10 22:13:34 +00:00
foreach ( string nodeId in Path . Split ( ',' ) )
{
2009-06-19 07:39:16 +00:00
if ( _cruds . ContainsKey ( int . Parse ( nodeId ) ) )
cruds = _cruds [ int . Parse ( nodeId ) ] . ToString ( ) ;
}
return cruds ;
}
/// <summary>
/// Initializes the user node permissions
/// </summary>
2010-06-10 22:13:34 +00:00
public void initCruds ( )
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
// clear cruds
System . Web . HttpContext . Current . Application . Lock ( ) ;
_cruds . Clear ( ) ;
System . Web . HttpContext . Current . Application . UnLock ( ) ;
2010-06-10 22:13:34 +00:00
using ( IRecordsReader dr = SqlHelper . ExecuteReader ( "select * from umbracoUser2NodePermission where userId = @userId order by nodeId" , SqlHelper . CreateParameter ( "@userId" , this . Id ) ) )
{
2009-06-19 07:39:16 +00:00
// int currentId = -1;
2010-06-10 22:13:34 +00:00
while ( dr . Read ( ) )
{
2009-06-19 07:39:16 +00:00
if ( ! _cruds . ContainsKey ( dr . GetInt ( "nodeId" ) ) )
_cruds . Add ( dr . GetInt ( "nodeId" ) , String . Empty ) ;
_cruds [ dr . GetInt ( "nodeId" ) ] + = dr . GetString ( "permission" ) ;
}
}
_crudsInitialized = true ;
}
/// <summary>
/// Gets a users notifications for a specified node path.
/// </summary>
/// <param name="Path">The node path.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public string GetNotifications ( string Path )
{
2009-06-19 07:39:16 +00:00
string notifications = "" ;
if ( ! _notificationsInitialized )
initNotifications ( ) ;
2010-06-10 22:13:34 +00:00
foreach ( string nodeId in Path . Split ( ',' ) )
{
2009-06-19 07:39:16 +00:00
if ( _notifications . ContainsKey ( int . Parse ( nodeId ) ) )
notifications = _notifications [ int . Parse ( nodeId ) ] . ToString ( ) ;
}
return notifications ;
}
/// <summary>
/// Clears the internal hashtable containing cached information about notifications for the user
/// </summary>
2010-06-10 22:13:34 +00:00
public void resetNotificationCache ( )
{
2009-06-19 07:39:16 +00:00
_notificationsInitialized = false ;
_notifications . Clear ( ) ;
}
/// <summary>
/// Initializes the notifications and caches them.
/// </summary>
2010-06-10 22:13:34 +00:00
public void initNotifications ( )
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
2010-06-10 22:13:34 +00:00
using ( IRecordsReader dr = SqlHelper . ExecuteReader ( "select * from umbracoUser2NodeNotify where userId = @userId order by nodeId" , SqlHelper . CreateParameter ( "@userId" , this . Id ) ) )
{
while ( dr . Read ( ) )
{
2009-06-19 07:39:16 +00:00
int nodeId = dr . GetInt ( "nodeId" ) ;
if ( ! _notifications . ContainsKey ( nodeId ) )
_notifications . Add ( nodeId , String . Empty ) ;
_notifications [ nodeId ] + = dr . GetString ( "action" ) ;
}
}
_notificationsInitialized = true ;
}
/// <summary>
/// Gets the user id.
/// </summary>
/// <value>The id.</value>
2010-06-10 22:13:34 +00:00
public int Id
{
2009-06-19 07:39:16 +00:00
get { return _id ; }
}
/// <summary>
/// Clears the list of applications the user has access to.
/// </summary>
2010-06-10 22:13:34 +00:00
public void clearApplications ( )
{
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( "delete from umbracoUser2app where [user] = @id" , SqlHelper . CreateParameter ( "@id" , this . Id ) ) ;
}
/// <summary>
/// Adds a application to the list of allowed applications
/// </summary>
/// <param name="AppAlias">The app alias.</param>
2010-06-10 22:13:34 +00:00
public void addApplication ( string AppAlias )
{
2009-06-19 07:39:16 +00:00
SqlHelper . ExecuteNonQuery ( "insert into umbracoUser2app ([user],app) values (@id, @app)" , SqlHelper . CreateParameter ( "@id" , this . Id ) , SqlHelper . CreateParameter ( "@app" , AppAlias ) ) ;
}
/// <summary>
/// Gets or sets a value indicating whether the user has access to the Umbraco back end.
/// </summary>
/// <value><c>true</c> if the user has access to the back end; otherwise, <c>false</c>.</value>
2010-06-10 22:13:34 +00:00
public bool NoConsole
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _userNoConsole ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_userNoConsole = value ;
SqlHelper . ExecuteNonQuery ( "update umbracoUser set userNoConsole = @userNoConsole where id = @id" , SqlHelper . CreateParameter ( "@id" , this . Id ) , SqlHelper . CreateParameter ( "@userNoConsole" , _userNoConsole ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="User"/> is disabled.
/// </summary>
/// <value><c>true</c> if disabled; otherwise, <c>false</c>.</value>
public bool Disabled
{
get
{
if ( ! _isInitialized )
setupUser ( _id ) ;
return _userDisabled ;
}
set
{
_userDisabled = value ;
SqlHelper . ExecuteNonQuery ( "update umbracoUser set userDisabled = @userDisabled where id = @id" , SqlHelper . CreateParameter ( "@id" , this . Id ) , SqlHelper . CreateParameter ( "@userDisabled" , _userDisabled ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets a value indicating whether a user should be redirected to liveediting by default.
/// </summary>
/// <value>
/// <c>true</c> if defaults to live editing; otherwise, <c>false</c>.
/// </value>
public bool DefaultToLiveEditing
{
get
{
if ( ! _isInitialized )
setupUser ( _id ) ;
return _defaultToLiveEditing ;
}
set
{
_defaultToLiveEditing = value ;
SqlHelper . ExecuteNonQuery ( "update umbracoUser set defaultToLiveEditing = @defaultToLiveEditing where id = @id" , SqlHelper . CreateParameter ( "@id" , this . Id ) , SqlHelper . CreateParameter ( "@defaultToLiveEditing" , _defaultToLiveEditing ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets the start content node id.
/// </summary>
/// <value>The start node id.</value>
2010-06-10 22:13:34 +00:00
public int StartNodeId
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _startnodeid ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_startnodeid = value ;
SqlHelper . ExecuteNonQuery ( "update umbracoUser set startStructureId = @start where id = @id" , SqlHelper . CreateParameter ( "@start" , value ) , SqlHelper . CreateParameter ( "@id" , this . Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Gets or sets the start media id.
/// </summary>
/// <value>The start media id.</value>
2010-06-10 22:13:34 +00:00
public int StartMediaId
{
get
{
2009-06-19 07:39:16 +00:00
if ( ! _isInitialized )
setupUser ( _id ) ;
return _startmediaid ;
}
2010-06-10 22:13:34 +00:00
set
{
2009-06-19 07:39:16 +00:00
_startmediaid = value ;
SqlHelper . ExecuteNonQuery ( "update umbracoUser set startMediaId = @start where id = @id" , SqlHelper . CreateParameter ( "@start" , value ) , SqlHelper . CreateParameter ( "@id" , this . Id ) ) ;
FlushFromCache ( ) ;
}
}
/// <summary>
/// Flushes the user from cache.
/// </summary>
2010-06-10 22:13:34 +00:00
protected void FlushFromCache ( )
{
2009-06-19 07:39:16 +00:00
OnFlushingFromCache ( EventArgs . Empty ) ;
if ( System . Web . HttpRuntime . Cache [ string . Format ( "UmbracoUser{0}" , Id . ToString ( ) ) ] ! = null )
System . Web . HttpRuntime . Cache . Remove ( string . Format ( "UmbracoUser{0}" , Id . ToString ( ) ) ) ;
}
/// <summary>
/// Gets the user with a specified ID
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
2010-06-10 22:13:34 +00:00
public static User GetUser ( int id )
{
if ( System . Web . HttpRuntime . Cache [ string . Format ( "UmbracoUser{0}" , id . ToString ( ) ) ] = = null )
{
2010-05-27 14:17:56 +00:00
try
{
User u = new User ( id ) ;
System . Web . HttpRuntime . Cache . Insert ( string . Format ( "UmbracoUser{0}" , id . ToString ( ) ) , u ) ;
}
catch ( ArgumentException )
{
//no user was found
return null ;
2010-06-10 22:13:34 +00:00
}
2009-06-19 07:39:16 +00:00
}
return ( User ) System . Web . HttpRuntime . Cache [ string . Format ( "UmbracoUser{0}" , id . ToString ( ) ) ] ;
}
//EVENTS
/// <summary>
/// The save event handler
/// </summary>
public delegate void SavingEventHandler ( User sender , EventArgs e ) ;
/// <summary>
/// The new event handler
/// </summary>
public delegate void NewEventHandler ( User sender , EventArgs e ) ;
/// <summary>
/// The disable event handler
/// </summary>
public delegate void DisablingEventHandler ( User sender , EventArgs e ) ;
/// <summary>
/// The delete event handler
/// </summary>
public delegate void DeletingEventHandler ( User sender , EventArgs e ) ;
/// <summary>
/// The Flush User from cache event handler
/// </summary>
public delegate void FlushingFromCacheEventHandler ( User sender , EventArgs e ) ;
/// <summary>
/// Occurs when [saving].
/// </summary>
public static event SavingEventHandler Saving ;
/// <summary>
/// Raises the <see cref="E:Saving"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
2010-06-10 22:13:34 +00:00
protected virtual void OnSaving ( EventArgs e )
{
2009-06-19 07:39:16 +00:00
if ( Saving ! = null )
Saving ( this , e ) ;
}
/// <summary>
/// Occurs when [new].
/// </summary>
public static event NewEventHandler New ;
/// <summary>
/// Raises the <see cref="E:New"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
2010-06-10 22:13:34 +00:00
protected virtual void OnNew ( EventArgs e )
{
2009-06-19 07:39:16 +00:00
if ( New ! = null )
New ( this , e ) ;
}
/// <summary>
/// Occurs when [disabling].
/// </summary>
public static event DisablingEventHandler Disabling ;
/// <summary>
/// Raises the <see cref="E:Disabling"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
2010-06-10 22:13:34 +00:00
protected virtual void OnDisabling ( EventArgs e )
{
2009-06-19 07:39:16 +00:00
if ( Disabling ! = null )
Disabling ( this , e ) ;
}
/// <summary>
/// Occurs when [deleting].
/// </summary>
public static event DeletingEventHandler Deleting ;
/// <summary>
/// Raises the <see cref="E:Deleting"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
2010-06-10 22:13:34 +00:00
protected virtual void OnDeleting ( EventArgs e )
{
2009-06-19 07:39:16 +00:00
if ( Deleting ! = null )
Deleting ( this , e ) ;
}
/// <summary>
/// Occurs when [flushing from cache].
/// </summary>
public static event FlushingFromCacheEventHandler FlushingFromCache ;
/// <summary>
/// Raises the <see cref="E:FlushingFromCache"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
2010-06-10 22:13:34 +00:00
protected virtual void OnFlushingFromCache ( EventArgs e )
{
2009-06-19 07:39:16 +00:00
if ( FlushingFromCache ! = null )
FlushingFromCache ( this , e ) ;
}
}
}