use of org.eclipse.kapua.service.user.User in project kapua by eclipse.
the class Users method getAccount.
/**
* Returns the User specified by the "userId" path parameter.
*
* @param userId
* The id of the User requested.
* @return The requested User object.
*/
@GET
@Path("{userId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public User getAccount(@PathParam("userId") String userId) {
User user = null;
try {
KapuaId id = KapuaEid.parseShortId(userId);
user = userService.find(KapuaSecurityUtils.getSession().getScopeId(), id);
} catch (Throwable t) {
handleException(t);
}
return returnNotNullEntity(user);
}
use of org.eclipse.kapua.service.user.User in project kapua by eclipse.
the class GwtAuthorizationServiceImpl method establishSession.
private GwtSession establishSession() throws KapuaException {
KapuaLocator locator = KapuaLocator.getInstance();
//
// Get info from session
KapuaSession kapuaSession = KapuaSecurityUtils.getSession();
//
// Get user info
UserService userService = locator.getService(UserService.class);
User user = userService.find(kapuaSession.getScopeId(), kapuaSession.getUserId());
//
// Get permission info
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
boolean hasAccountCreate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
boolean hasAccountRead = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.read, kapuaSession.getScopeId()));
boolean hasAccountUpdate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
boolean hasAccountDelete = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.delete, kapuaSession.getScopeId()));
boolean hasAccountAll = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, null, null));
boolean hasDeviceCreate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
boolean hasDeviceRead = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.read, kapuaSession.getScopeId()));
boolean hasDeviceUpdate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
boolean hasDeviceDelete = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.delete, kapuaSession.getScopeId()));
boolean hasDeviceManage = authorizationService.isPermitted(permissionFactory.newPermission(DeviceLifecycleDomain.DEVICE_LIFECYCLE, Actions.write, kapuaSession.getScopeId()));
boolean hasDataRead = authorizationService.isPermitted(permissionFactory.newPermission("data", Actions.read, kapuaSession.getScopeId()));
boolean hasUserCreate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
boolean hasUserRead = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.read, kapuaSession.getScopeId()));
boolean hasUserUpdate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
boolean hasUserDelete = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.delete, kapuaSession.getScopeId()));
//
// Get account info
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.find(kapuaSession.getScopeId());
//
// Convert entities
GwtUser gwtUser = KapuaGwtConverter.convert(user);
GwtAccount gwtAccount = KapuaGwtConverter.convert(account);
//
// Build the session
GwtSession gwtSession = new GwtSession();
// Console info
SystemSetting commonsConfig = SystemSetting.getInstance();
gwtSession.setVersion(commonsConfig.getString(SystemSettingKey.VERSION));
gwtSession.setBuildVersion(commonsConfig.getString(SystemSettingKey.BUILD_VERSION));
gwtSession.setBuildNumber(commonsConfig.getString(SystemSettingKey.BUILD_NUMBER));
// User info
gwtSession.setGwtUser(gwtUser);
gwtSession.setGwtAccount(gwtAccount);
gwtSession.setRootAccount(gwtAccount);
gwtSession.setSelectedAccount(gwtAccount);
// Permission info
gwtSession.setAccountCreatePermission(hasAccountCreate);
gwtSession.setAccountReadPermission(hasAccountRead);
gwtSession.setAccountUpdatePermission(hasAccountUpdate);
gwtSession.setAccountDeletePermission(hasAccountDelete);
gwtSession.setAccountAllPermission(hasAccountAll);
gwtSession.setDeviceCreatePermission(hasDeviceCreate);
gwtSession.setDeviceReadPermission(hasDeviceRead);
gwtSession.setDeviceUpdatePermission(hasDeviceUpdate);
gwtSession.setDeviceDeletePermission(hasDeviceDelete);
gwtSession.setDeviceManagePermission(hasDeviceManage);
gwtSession.setDataReadPermission(hasDataRead);
gwtSession.setUserCreatePermission(hasUserCreate);
gwtSession.setUserReadPermission(hasUserRead);
gwtSession.setUserUpdatePermission(hasUserUpdate);
gwtSession.setUserDeletePermission(hasUserDelete);
return gwtSession;
}
use of org.eclipse.kapua.service.user.User in project kapua by eclipse.
the class GwtAuthorizationServiceImpl method getCurrentSession.
/**
* Return the currently authenticated user or null if no session has been established.
*/
public GwtSession getCurrentSession() throws GwtKapuaException {
GwtSession gwtSession = null;
try {
Subject currentUser = SecurityUtils.getSubject();
if (currentUser != null && currentUser.isAuthenticated()) {
Session session = currentUser.getSession();
gwtSession = (GwtSession) session.getAttribute(SESSION_CURRENT);
// Store the user information in the sessions
String username = (String) currentUser.getPrincipal();
KapuaLocator locator = KapuaLocator.getInstance();
UserService userService = locator.getService(UserService.class);
User user = userService.findByName(username);
// get the session
if (gwtSession == null) {
gwtSession = establishSession();
} else {
gwtSession.setGwtUser(KapuaGwtConverter.convert(user));
}
}
} catch (Throwable t) {
s_logger.warn("Error in getCurrentSession.", t);
KapuaExceptionHandler.handle(t);
}
return gwtSession;
}
use of org.eclipse.kapua.service.user.User in project kapua by eclipse.
the class GwtUserServiceImpl method findAll.
public ListLoadResult<GwtUser> findAll(String scopeIdString) throws GwtKapuaException {
KapuaId scopeId = KapuaEid.parseShortId(scopeIdString);
List<GwtUser> gwtUserList = new ArrayList<GwtUser>();
try {
KapuaLocator locator = KapuaLocator.getInstance();
UserService userService = locator.getService(UserService.class);
UserFactory userFactory = locator.getFactory(UserFactory.class);
UserQuery query = userFactory.newQuery(scopeId);
UserListResult list = userService.query(query);
for (User user : list.getItems()) {
gwtUserList.add(KapuaGwtConverter.convert(user));
}
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return new BaseListLoadResult<GwtUser>(gwtUserList);
}
use of org.eclipse.kapua.service.user.User in project kapua by eclipse.
the class GwtUserServiceImpl method update.
public GwtUser update(GwtXSRFToken xsrfToken, GwtUser gwtUser) throws GwtKapuaException {
checkXSRFToken(xsrfToken);
GwtUser gwtUserUpdated = null;
try {
KapuaLocator locator = KapuaLocator.getInstance();
UserService userService = locator.getService(UserService.class);
KapuaId scopeId = KapuaEid.parseShortId(gwtUser.getScopeId());
KapuaId userId = KapuaEid.parseShortId(gwtUser.getId());
User user = userService.find(scopeId, userId);
if (user != null) {
//
// Update user
user.setName(gwtUser.getUnescapedUsername());
user.setDisplayName(gwtUser.getUnescapedDisplayName());
user.setEmail(gwtUser.getUnescapedEmail());
user.setPhoneNumber(gwtUser.getUnescapedPhoneNumber());
// status
user.setStatus(UserStatus.valueOf(gwtUser.getStatus()));
//
// Update permissions
Set<String> newPermissions = new HashSet<String>();
if (gwtUser.getPermissions() != null) {
// build the set of permissions
newPermissions.addAll(Arrays.asList(gwtUser.getPermissions().split(",")));
}
UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
Set<UserPermissionCreator> newUserPermissions = new HashSet<UserPermissionCreator>();
for (String p : newPermissions) {
UserPermissionCreator userPermissionCreator = userPermissionFactory.newCreator(user.getScopeId());
userPermissionCreator.setUserId(scopeId);
String[] tokens = p.split(":");
String domain = null;
Actions action = null;
KapuaId targetScopeId = null;
if (tokens.length > 0) {
domain = tokens[0];
}
if (tokens.length > 1) {
action = Actions.valueOf(tokens[1]);
}
if (tokens.length > 2) {
targetScopeId = KapuaEid.parseShortId(tokens[2]);
}
Permission permission = permissionFactory.newPermission(domain, action, targetScopeId);
userPermissionCreator.setPermission(permission);
userPermissionService.create(userPermissionCreator);
}
userPermissionService.merge(newUserPermissions);
// Update credentials
if (gwtUser.getPassword() != null) {
CredentialService credentialService = locator.getService(CredentialService.class);
CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class);
CredentialListResult credentials = credentialService.findByUserId(scopeId, userId);
if (!credentials.isEmpty()) {
//
// Delete old PASSWORD credential
Credential oldCredential = null;
for (Credential c : credentials.getItems()) {
if (CredentialType.PASSWORD.equals(c.getCredentialType())) {
oldCredential = c;
break;
}
}
credentialService.delete(oldCredential.getScopeId(), oldCredential.getId());
//
// Create new PASSWORD credential
CredentialCreator credentialCreator = credentialFactory.newCreator(scopeId, user.getId(), CredentialType.PASSWORD, gwtUser.getPassword());
credentialService.create(credentialCreator);
}
}
// optlock
user.setOptlock(gwtUser.getOptlock());
// update the user
userService.update(user);
//
// convert to GwtAccount and return
// reload the user as we want to load all its permissions
gwtUserUpdated = KapuaGwtConverter.convert(userService.find(user.getScopeId(), user.getId()));
}
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtUserUpdated;
}
Aggregations