bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
DbUser.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.db.mappers;
2 
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7 
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10 import org.openslx.bwlp.thrift.iface.Role;
11 import org.openslx.bwlp.thrift.iface.TNotFoundException;
12 import org.openslx.bwlp.thrift.iface.UserInfo;
18 
22 public class DbUser
23 {
24 
25  private static final Logger LOGGER = LogManager.getLogger( DbUser.class );
26 
27  private static final String localUserSql = "SELECT"
28  + " user.userid, user.password, user.organizationid, user.firstname, user.lastname, user.email"
29  + " FROM user";
30 
31  private static LocalUser localFromRs( ResultSet rs ) throws SQLException
32  {
33  return new LocalUser( rs.getString( "userid" ), rs.getString( "password" ),
34  rs.getString( "organizationid" ), rs.getString( "firstname" ), rs.getString( "lastname" ), rs.getString( "email" ),
35  Role.TUTOR );
36  }
37 
46  public static LocalUser forUserId( final String login ) throws SQLException
47  {
48  try ( MysqlConnection connection = Database.getConnection() ) {
49  MysqlStatement stmt = connection.prepareStatement( localUserSql
50  + " WHERE user.userid = :userid" );
51  stmt.setString( "userid", login );
52  ResultSet rs = stmt.executeQuery();
53  if ( !rs.next() )
54  return null;
55  return localFromRs( rs );
56  } catch ( SQLException e ) {
57  LOGGER.error( "Query failed in DbUser.forLogin()", e );
58  throw e;
59  }
60  }
61 
62  public static LocalUser forUserId( String login, String password ) throws SQLException
63  {
64  LocalUser user = forUserId( login );
65  if ( user == null || !Sha512Crypt.verifyPassword( password, user.password ) )
66  return null;
67  return user;
68  }
69 
70  public static UserInfo getUserInfo( final String login ) throws SQLException, TNotFoundException
71  {
72  LocalUser user = forUserId( login );
73  if ( user == null )
74  throw new TNotFoundException();
75  return user.toUserInfo();
76  }
77 
78  static UserInfo getUserInfoOrNull( MysqlConnection connection, String userId ) throws SQLException
79  {
80  MysqlStatement stmt = connection.prepareStatement( localUserSql
81  + " WHERE user.userid = :userid" );
82  stmt.setString( "userid", userId );
83  ResultSet rs = stmt.executeQuery();
84  if ( !rs.next() )
85  return null;
86  return localFromRs( rs ).toUserInfo();
87  }
88 
89  public static List<UserInfo> findUser( String organizationId, String searchTerm )
90  {
91  // TODO Implement
92  return new ArrayList<>( 0 );
93  }
94 
95  public static boolean exists( UserInfo user )
96  {
97  return exists( user, false );
98  }
99 
100  public static boolean exists( UserInfo user, boolean withIdentity )
101  {
102  if ( user == null )
103  return false;
104  return exists( user.userId, withIdentity );
105  }
106 
107  private static boolean exists( String userId, boolean withIdentitiy )
108  {
109  if ( userId == null )
110  return false;
111  try {
112  return forUserId( userId ) != null;
113  } catch ( SQLException e ) {
114  return false;
115  }
116  }
117 
118 }
static UserInfo getUserInfoOrNull(MysqlConnection connection, String userId)
Definition: DbUser.java:78
static List< UserInfo > findUser(String organizationId, String searchTerm)
Definition: DbUser.java:89
static boolean exists(UserInfo user, boolean withIdentity)
Definition: DbUser.java:100
static MysqlConnection getConnection()
Get a connection to the database.
Definition: Database.java:92
ResultSet executeQuery()
Executes the statement, which must be a query.
Represents a user that can login against the masterserver.
Definition: DbUser.java:22
static final boolean verifyPassword(String plaintextPass, String sha512CryptText)
static boolean exists(UserInfo user)
Definition: DbUser.java:95
static LocalUser forUserId(String login, String password)
Definition: DbUser.java:62
static UserInfo getUserInfo(final String login)
Definition: DbUser.java:70
void setString(String name, String value)
Sets a parameter.
static LocalUser forUserId(final String login)
Query database for user with given user id.
Definition: DbUser.java:46
Class for creating PreparedStatements with named parameters.
static boolean exists(String userId, boolean withIdentitiy)
Definition: DbUser.java:107
static LocalUser localFromRs(ResultSet rs)
Definition: DbUser.java:31