use of org.openremote.model.security.User in project openremote by openremote.
the class AssetStorageService method merge.
/**
* @param overrideVersion If <code>true</code>, the merge will override the data in the database, independent of version.
* @param userName the user which this asset needs to be assigned to.
* @return The current stored asset state.
* @throws IllegalArgumentException if the realm or parent is illegal, or other asset constraint is violated.
*/
public ServerAsset merge(ServerAsset asset, boolean overrideVersion, String userName) {
return persistenceService.doReturningTransaction(em -> {
// Update all empty attribute timestamps with server-time (a caller which doesn't have a
// reliable time source such as a browser should clear the timestamp when setting an attribute
// value).
asset.getAttributesStream().forEach(attribute -> {
Optional<Long> timestamp = attribute.getValueTimestamp();
if (!timestamp.isPresent() || timestamp.get() <= 0) {
attribute.setValueTimestamp(timerService.getCurrentTimeMillis());
}
});
// Validate parent
if (asset.getParentId() != null) {
// If this is a not a root asset...
ServerAsset parent = find(em, asset.getParentId(), true);
// .. the parent must exist
if (parent == null)
throw new IllegalStateException("Parent not found: " + asset.getParentId());
// ... the parent can not be a child of the asset
if (parent.pathContains(asset.getId()))
throw new IllegalStateException("Invalid parent");
// .. the parent should be in the same realm
if (asset.getRealmId() != null && !parent.getRealmId().equals(asset.getRealmId())) {
throw new IllegalStateException("Parent not in same realm as asset: " + asset.getRealmId());
} else if (asset.getRealmId() == null) {
// ... and if we don't have a realm identifier, use the parent's
asset.setRealmId(parent.getRealmId());
}
}
// Validate realm
if (!identityService.getIdentityProvider().isActiveTenant(asset.getRealmId())) {
throw new IllegalStateException("Realm not found/active: " + asset.getRealmId());
}
// Validate attributes
int invalid = 0;
for (AssetAttribute attribute : asset.getAttributesList()) {
List<ValidationFailure> validationFailures = attribute.getValidationFailures();
if (!validationFailures.isEmpty()) {
LOG.warning("Validation failure(s) " + validationFailures + ", can't store: " + attribute);
invalid++;
}
}
if (invalid > 0) {
throw new IllegalStateException("Storing asset failed, invalid attributes: " + invalid);
}
// concurrent updates
if (asset.getId() != null && overrideVersion) {
ServerAsset existing = em.find(ServerAsset.class, asset.getId());
if (existing != null) {
asset.setVersion(existing.getVersion());
}
}
// If username present
User user = null;
if (!TextUtil.isNullOrEmpty(userName)) {
user = identityService.getIdentityProvider().getUser(asset.getRealmId(), userName);
if (user == null) {
throw new IllegalStateException("User not found: " + userName);
}
}
LOG.fine("Storing: " + asset);
ServerAsset updatedAsset = em.merge(asset);
if (user != null) {
storeUserAsset(em, new UserAsset(user.getRealmId(), user.getId(), updatedAsset.getId()));
}
return updatedAsset;
});
}
use of org.openremote.model.security.User in project openremote by openremote.
the class AssetsTenantImpl method setUsers.
@Override
public void setUsers(User[] users) {
usersListBox.clear();
usersListBox.addItem(managerMessages.noUserSelected(), "");
if (users.length == 0) {
usersListBox.addItem(managerMessages.noUserFound(), "");
}
for (User user : users) {
usersListBox.addItem(user.getUsername() + " (" + user.getFirstName() + " " + user.getLastName() + ")", user.getUsername());
}
}
use of org.openremote.model.security.User in project openremote by openremote.
the class AdminUserEditActivity method start.
@Override
public void start(AcceptsView container, EventBus eventBus, Collection<EventRegistration> registrations) {
super.start(container, eventBus, registrations);
adminContent.setPresenter(this);
adminContent.clearRoles();
adminContent.clearFormMessages();
clearViewFieldErrors();
adminContent.enableCreate(false);
adminContent.enableUpdate(false);
adminContent.enableDelete(false);
adminContent.enableResetPassword(false);
adminContent.enableRoles(false);
adminContent.setEditMode(false);
if (userId != null) {
adminContent.setEditMode(true);
loadUser();
} else {
user = new User();
user.setRealm(realm);
writeToView();
adminContent.enableCreate(true);
}
}
Aggregations