summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java
blob: 9cde27338fc88e2725b39de8c8e9fe7b74f4c885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.openslx.imagemaster.db.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Role;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.imagemaster.db.Database;
import org.openslx.imagemaster.db.MysqlConnection;
import org.openslx.imagemaster.db.MysqlStatement;
import org.openslx.imagemaster.db.models.LocalUser;
import org.openslx.imagemaster.util.Sha512Crypt;

/**
 * Represents a user that can login against the masterserver.
 */
public class DbUser
{

	private static final Logger LOGGER = Logger.getLogger( DbUser.class );

	private static final String localUserSql = "SELECT"
			+ " user.userid, user.password, user.organizationid, user.firstname, user.lastname, user.email"
			+ " FROM user";

	private static LocalUser localFromRs( ResultSet rs ) throws SQLException
	{
		return new LocalUser( rs.getString( "userid" ), rs.getString( "password" ),
				rs.getString( "organizationid" ), rs.getString( "firstname" ), rs.getString( "lastname" ), rs.getString( "email" ),
				Role.TUTOR );
	}

	/**
	 * Query database for user with given login
	 * 
	 * @param login (global user-id, login@org for test-accounts)
	 * @return instance of DbUser for matching entry from DB, or null if not
	 *         found
	 * @throws SQLException if the query fails
	 */
	public static LocalUser forLogin( final String login ) throws SQLException
	{
		try ( MysqlConnection connection = Database.getConnection() ) {
			MysqlStatement stmt = connection.prepareStatement( localUserSql
					+ " WHERE user.userid = :userid" );
			stmt.setString( "userid", login );
			ResultSet rs = stmt.executeQuery();
			if ( !rs.next() )
				return null;
			return localFromRs( rs );
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbUser.forLogin()", e );
			throw e;
		}
	}

	public static UserInfo getUserInfo( final String login ) throws SQLException, TNotFoundException
	{
		LocalUser user = forLogin( login );
		if ( user == null )
			throw new TNotFoundException();
		return user.toUserInfo();
	}

	public static LocalUser forLogin( String login, String password ) throws SQLException
	{
		LocalUser user = forLogin( login );
		if ( user == null || !Sha512Crypt.verifyPassword( password, user.password ) )
			return null;
		return user;
	}

	public static List<UserInfo> findUser( String organizationId, String searchTerm )
	{
		// TODO Implement
		return new ArrayList<>( 0 );
	}

	public static boolean exists( UserInfo user )
	{
		if ( user == null )
			return false;
		return exists( user.userId );
	}

	private static boolean exists( String userId )
	{
		if ( userId == null )
			return false;
		try {
			return forLogin( userId ) != null;
		} catch ( SQLException e ) {
			return false;
		}
	}

}