summaryrefslogtreecommitdiff
path: root/src/lq/UserUtil.java
blob: ae882d34642704ec6143d0cd493c357ed83569fa (plain)
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
package lq;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

public class UserUtil {
    public static final String getAuthenticatedEmail() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user == null) {
            return null;
        }
        else {
            if (userExists(user.getEmail())) {
              return user.getEmail();
            }
            else {
              return null;
            }
        }
    }

    public static final boolean isAuthenticated() {
        return getAuthenticatedEmail() != null;
    }

    public static final String getLoginUrl(String requestUrl) {
        UserService userService = UserServiceFactory.getUserService();
        return userService.createLoginURL(requestUrl);
    }

    public static final String getLogoutUrl(String requestUrl) {
        UserService userService = UserServiceFactory.getUserService();
        return userService.createLogoutURL(requestUrl);
    }

    public static final boolean hasZeroUsers() {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Query query = pm.newQuery(Administrator.class);
            List<?> results = (List<?>) query.execute();
            return results.isEmpty();
        }
        finally {
            pm.close();
        }
    }

    public static final boolean userExists(String email) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            Query query = pm.newQuery(Administrator.class);
            query.setFilter("email == emailParam");
            query.declareParameters("String emailParam");
            List<?> results = (List<?>) query.execute(email);
            return !results.isEmpty();
        }
        finally {
            pm.close();
        }

    }
}