1
2
3
4
5
6
7
8
9 package com.buckosoft.BSAccount;
10
11 import javax.servlet.http.HttpServletRequest;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.springframework.web.util.WebUtils;
16
17 import com.buckosoft.BSAccount.db.Database;
18 import com.buckosoft.BSAccount.db.DatabaseImpl;
19 import com.buckosoft.BSAccount.domain.BSAccount;
20 import com.buckosoft.BSAccount.domain.BSAccountUser;
21 import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
22 import com.buckosoft.BSAccount.domain.UserFactory;
23
24
25
26
27
28 public class BSAccountManImpl implements BSAccountMan {
29 protected final Log log = LogFactory.getLog(getClass());
30 private Database db;
31 private UserFactory userFactory = null;
32 private String appName = "unknownApp";
33 private boolean helloForwards = false;
34
35
36
37
38 public void setUserFactory(UserFactory userFactory) {
39 this.userFactory = userFactory;
40 }
41
42
43
44
45 public BSAccountUser getNewUser() {
46 return(this.userFactory.getNewUser());
47 }
48 public BSAccountUser getUser(BSAccount account) {
49 return(this.userFactory.getUser(account));
50 }
51
52
53
54
55 public BSAccountUserWebSession getNewUserWebSession(BSAccountUser user) {
56 return(this.userFactory.getNewUserWebSession(user));
57 }
58
59
60
61
62
63 public BSAccountUserWebSession getUserWebSession(HttpServletRequest request) {
64 BSAccountUserWebSession userWebSession = (BSAccountUserWebSession) WebUtils.getSessionAttribute(request, "userWebSession");
65 BSAccountUser user;
66 if (userWebSession == null) {
67 user = this.getNewUser();
68 userWebSession = this.getNewUserWebSession(user);
69 request.getSession().setAttribute("userWebSession", userWebSession);
70 }
71 String s = request.getParameter("jumpApp");
72 if (s != null) {
73 long l = Long.parseLong(s);
74 int id = db.getAppJumperUser(l);
75 if (id == 0) {
76 log.warn("jumpApp failed for long " + l);
77 return(userWebSession);
78 }
79 db.deleteAppJumper(id, l);
80 BSAccount a = db.getAccount(id);
81 userWebSession.getUser().setAccount(a);
82 log.info("Coercing login for " + a.getUsername());
83 }
84 return(userWebSession);
85 }
86
87 @Override
88 public long getAppJumper(BSAccount account) {
89 if (!account.isRegisteredUser())
90 return(0);
91 int i = (int)(Math.random() * Integer.MAX_VALUE);
92 long l = (int)(Math.random() * Integer.MAX_VALUE) + (long)((long)i << Integer.SIZE);
93 db.setAppJumper(account.getUserId(), l);
94 return l;
95 }
96
97 @Override
98 public boolean isUserWebSession(HttpServletRequest request) {
99 BSAccountUserWebSession userWebSession = (BSAccountUserWebSession) WebUtils.getSessionAttribute(request, "userWebSession");
100 return(userWebSession != null);
101 }
102
103
104
105
106 public String getAppName() {
107 return(this.appName);
108 }
109
110
111
112
113 public void setAppName(String appName) {
114 this.appName = appName;
115 }
116
117
118
119
120
121 public boolean isHelloForwards() {
122 return(this.helloForwards);
123 }
124
125
126
127
128 public void setHelloForwards(boolean helloForwards) {
129 this.helloForwards = helloForwards;
130 }
131
132
133 public void setInit(String unused) {
134 log.info("Init");
135
136
137
138 db = new DatabaseImpl();
139 if (this.userFactory == null) {
140 this.userFactory = new SimpleUserFactory();
141 log.info("Using default SimpleUserFactory");
142 }
143
144 log.info("Init done");
145 }
146
147
148
149
150 public BSAccount getAccount(String username, String password) {
151 log.info("Get account for " + username + " / xxx");
152 BSAccount account = db.getAccount(username, password);
153 return(account);
154 }
155
156 }