summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util/UserUtil.java
blob: 2e447f3056bf47b9b1eebb10871968fc28eddf34 (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
package org.openslx.imagemaster.util;

import java.sql.SQLException;

import org.openslx.bwlp.thrift.iface.AuthorizationError;
import org.openslx.bwlp.thrift.iface.Role;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.imagemaster.db.mappers.DbUser;

public class UserUtil
{

	/**
	 * Given a list of users, return the first one that isn't anonymous, which means they opted in
	 * for global image sharing.
	 */
	public static UserInfo getFirstPublishingUser( UserInfo... user )
	{
		if ( user == null )
			return null;
		for ( UserInfo u : user ) {
			if ( Util.isEmpty( u.userId ) )
				continue;
			try {
				u = DbUser.getUserInfo( u.userId );
			} catch ( SQLException | TNotFoundException e ) {
				continue;
			}
			if ( !Util.isEmpty( u.eMail )
					&& ( !Util.isEmpty( u.firstName ) || !Util.isEmpty( u.lastName ) ) ) {
				return u;
			}
		}
		return null;
	}

	/**
	 * Given a list of users, return the first one that isn't anonymous, which means they opted in
	 * for global image sharing. If none matches, return the dummy user.
	 */
	public static UserInfo getFirstPublishingUserOrDummy( UserInfo... user )
	{
		UserInfo ret = getFirstPublishingUser( user );
		if ( ret != null )
			return ret;
		try {
			return DbUser.getUserInfo( "dummy" );
		} catch ( TNotFoundException | SQLException e ) {
			return null;
		}
	}

	public static void assertTutor( UserInfo userInfo ) throws TAuthorizationException
	{
		if ( userInfo == null || userInfo.role != Role.TUTOR ) {
			throw new TAuthorizationException( AuthorizationError.NO_PERMISSION, "Permission denied!" );
		}
	}

}