66 lines
1.6 KiB
C#
66 lines
1.6 KiB
C#
using Microsoft.Data.SqlClient;
|
|
|
|
namespace Library;
|
|
|
|
public class UserData
|
|
{
|
|
public int ID = 0;
|
|
public string Name = default!;
|
|
public string Email = default!;
|
|
public string SessionID = default!;
|
|
public bool IsActive = true;
|
|
public bool IsAdmin = false;
|
|
|
|
public static UserData? FromSession( SqlConnection connection , string session_id )
|
|
{
|
|
using SqlDataReader reader = new SQLBuilder( connection , "EXEC [usp_user_session_validate] @session_id" )
|
|
.AddParameter( "@session_id" , session_id )
|
|
.ExecuteReader( );
|
|
|
|
if ( reader == null || !reader.Read( ) )
|
|
return null;
|
|
|
|
int user_id = reader.Int( "id" , 0 );
|
|
if ( user_id <= 0 )
|
|
return null;
|
|
|
|
return new( )
|
|
{
|
|
ID = user_id ,
|
|
Name = reader.String( "display_name" ) ?? "" ,
|
|
Email = reader.String( "email" ) ?? "" ,
|
|
SessionID = session_id ,
|
|
IsAdmin = reader.Int( "is_admin" , 0 ) == 1 ,
|
|
IsActive = true
|
|
};
|
|
}
|
|
|
|
public static UserData? FromID( SqlConnection connection , int id )
|
|
{
|
|
using SqlDataReader reader = new SQLBuilder( connection , "SELECT [id],[ime],[user_tip],[mail],[is_active] FROM [dom] WHERE [id]=@id" )
|
|
.AddParameter( "@id" , id )
|
|
.ExecuteReader( );
|
|
|
|
if ( reader == null || !reader.Read( ) )
|
|
return null;
|
|
|
|
return FromReader( reader );
|
|
}
|
|
|
|
public static UserData? FromReader( SqlDataReader reader )
|
|
{
|
|
int user_id = reader.Int( "id" , 0 );
|
|
if ( user_id <= 0 )
|
|
return null;
|
|
|
|
return new( )
|
|
{
|
|
ID = user_id ,
|
|
Name = reader.String( "ime" ) ?? "" ,
|
|
Email = reader.String( "mail" ) ?? "" ,
|
|
IsAdmin = ( reader.String( "user_tip" ) ?? "" ) == "admin" ,
|
|
IsActive = reader.Int( "is_active" , 0 ) != 0
|
|
};
|
|
}
|
|
}
|