This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
AccountService.java
189 lines (158 loc) · 5.17 KB
/
AccountService.java
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.zanata.rest.service;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.hibernate.Session;
import org.jboss.resteasy.spi.NoLogWebApplicationException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.jboss.seam.security.Identity;
import org.zanata.common.LocaleId;
import org.zanata.dao.AccountDAO;
import org.zanata.dao.AccountRoleDAO;
import org.zanata.dao.LocaleDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HAccountRole;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.rest.MediaTypes;
import org.zanata.rest.dto.Account;
import org.zanata.seam.resteasy.IgnoreInterfacePath;
@Name(AccountResource.SERVICE_PATH)
@Path("/accounts/u/{username:[a-z\\d_]{3,20}}")
@Transactional
@IgnoreInterfacePath
public class AccountService implements AccountResource
{
/** User name that identifies an account. */
@PathParam("username")
String username;
@Context
private HttpServletRequest request;
@Context
private UriInfo uri;
Log log = Logging.getLog(AccountService.class);
@In
private AccountDAO accountDAO;
@In
private AccountRoleDAO accountRoleDAO;
@In
private LocaleDAO localeDAO;
@In
private Identity identity;
@In
private Session session;
@Override
@GET
@Produces(
{MediaTypes.APPLICATION_ZANATA_ACCOUNT_XML, MediaTypes.APPLICATION_ZANATA_ACCOUNT_JSON})
public Response get()
{
log.debug("HTTP GET {0}", request.getRequestURL());
HAccount hAccount = accountDAO.getByUsername(username);
if (hAccount == null)
{
return Response.status(Status.NOT_FOUND).entity("Username not found").build();
}
Account result = new Account();
transfer(hAccount, result);
log.debug("HTTP GET result :\n" + result);
return Response.ok(result).build();
}
@Override
@PUT
@Consumes(
{MediaTypes.APPLICATION_ZANATA_ACCOUNT_XML, MediaTypes.APPLICATION_ZANATA_ACCOUNT_JSON})
public Response put(Account account)
{
log.debug("HTTP PUT {0} : \n{1}", request.getRequestURL(), account);
//RestUtils.validateEntity(account);
HAccount hAccount = accountDAO.getByUsername(username);
ResponseBuilder response;
String operation;
if (hAccount == null)
{
// creating
operation = "insert";
response = Response.created(uri.getAbsolutePath());
hAccount = new HAccount();
HPerson person = new HPerson();
person.setAccount(hAccount);
hAccount.setPerson(person);
}
else
{
// updating
operation = "update";
response = Response.ok();
}
transfer(account, hAccount);
// entity permission check
identity.checkPermission(hAccount, operation);
session.save(hAccount);
session.flush();
return response.build();
}
private void transfer(Account from, HAccount to)
{
to.setApiKey(from.getApiKey());
to.setEnabled(from.isEnabled());
to.setPasswordHash(from.getPasswordHash());
HPerson hPerson = to.getPerson();
hPerson.setEmail(from.getEmail());
hPerson.setName(from.getName());
to.getRoles().clear();
for (String role : from.getRoles())
{
HAccountRole hAccountRole = accountRoleDAO.findByName(role);
if (hAccountRole == null)
{
// generate error for missing role
log.debug("Invalid role '{0}'", role);
throw new NoLogWebApplicationException(Response.status(Status.BAD_REQUEST).entity("Invalid role '"+role+"'").build());
}
to.getRoles().add(hAccountRole);
}
hPerson.getLanguageMemberships().clear();
for (String tribe : from.getTribes())
{
HLocale hTribe = localeDAO.findByLocaleId(new LocaleId(tribe));
if (hTribe == null)
// generate error for missing tribe
throw new NoLogWebApplicationException(Response.status(Status.BAD_REQUEST).entity(
"Invalid tribe '" + tribe + "'").build());
hPerson.getLanguageMemberships().add(hTribe);
}
to.setUsername(from.getUsername());
}
private void transfer(HAccount from, Account to)
{
to.setApiKey(from.getApiKey());
to.setEnabled(from.isEnabled());
to.setPasswordHash(from.getPasswordHash());
HPerson hPerson = from.getPerson();
to.setEmail(hPerson.getEmail());
to.setName(hPerson.getName());
Set<String> roles = new HashSet<String>();
for (HAccountRole role : from.getRoles())
{
roles.add(role.getName());
}
to.setRoles(roles);
to.setUsername(from.getUsername());
}
}