use of com.orientechnologies.orient.core.metadata.schema.OClass in project orientdb by orientechnologies.
the class OSecurityShared method createMetadata.
/**
* Repairs the security structure if broken by creating the ADMIN role and user with default password.
*
* @return
*/
public OUser createMetadata() {
final ODatabaseDocument database = getDatabase();
// SINCE 1.2.0
OClass identityClass = database.getMetadata().getSchema().getClass(OIdentity.CLASS_NAME);
if (identityClass == null)
identityClass = database.getMetadata().getSchema().createAbstractClass(OIdentity.CLASS_NAME);
OClass roleClass = createOrUpdateORoleClass(database, identityClass);
createOrUpdateOUserClass(database, identityClass, roleClass);
// CREATE ROLES AND USERS
ORole adminRole = getRole(ORole.ADMIN);
if (adminRole == null) {
adminRole = createRole(ORole.ADMIN, ORole.ALLOW_MODES.ALLOW_ALL_BUT);
adminRole.addRule(ORule.ResourceGeneric.BYPASS_RESTRICTED, null, ORole.PERMISSION_ALL).save();
}
OUser adminUser = getUser(OUser.ADMIN);
if (adminUser == null) {
// This will return the global value if a local storage context configuration value does not exist.
boolean createDefUsers = getDatabase().getStorage().getConfiguration().getContextConfiguration().getValueAsBoolean(OGlobalConfiguration.CREATE_DEFAULT_USERS);
if (createDefUsers) {
adminUser = createUser(OUser.ADMIN, OUser.ADMIN, adminRole);
}
}
// SINCE 1.2.0
createOrUpdateORestrictedClass(database);
return adminUser;
}
use of com.orientechnologies.orient.core.metadata.schema.OClass in project orientdb by orientechnologies.
the class OSecurityShared method createOrUpdateORoleClass.
private OClass createOrUpdateORoleClass(final ODatabaseDocument database, OClass identityClass) {
OClass roleClass = database.getMetadata().getSchema().getClass("ORole");
boolean unsafe = false;
if (roleClass == null) {
roleClass = database.getMetadata().getSchema().createClass("ORole", identityClass);
unsafe = true;
} else if (!roleClass.getSuperClasses().contains(identityClass))
// MIGRATE AUTOMATICALLY TO 1.2.0
roleClass.setSuperClasses(Arrays.asList(identityClass));
if (!roleClass.existsProperty("name")) {
roleClass.createProperty("name", OType.STRING, (OType) null, unsafe).setMandatory(true).setNotNull(true).setCollate("ci");
roleClass.createIndex("ORole.name", INDEX_TYPE.UNIQUE, ONullOutputListener.INSTANCE, "name");
} else {
final OProperty name = roleClass.getProperty("name");
if (name.getAllIndexes().isEmpty())
roleClass.createIndex("ORole.name", INDEX_TYPE.UNIQUE, ONullOutputListener.INSTANCE, "name");
}
if (!roleClass.existsProperty("mode"))
roleClass.createProperty("mode", OType.BYTE, (OType) null, unsafe);
if (!roleClass.existsProperty("rules"))
roleClass.createProperty("rules", OType.EMBEDDEDMAP, OType.BYTE, unsafe);
if (!roleClass.existsProperty("inheritedRole"))
roleClass.createProperty("inheritedRole", OType.LINK, roleClass, unsafe);
return roleClass;
}
use of com.orientechnologies.orient.core.metadata.schema.OClass in project orientdb by orientechnologies.
the class OSecurityShared method createOrUpdateORestrictedClass.
private void createOrUpdateORestrictedClass(final ODatabaseDocument database) {
OClass restrictedClass = database.getMetadata().getSchema().getClass(RESTRICTED_CLASSNAME);
boolean unsafe = false;
if (restrictedClass == null) {
restrictedClass = database.getMetadata().getSchema().createAbstractClass(RESTRICTED_CLASSNAME);
unsafe = true;
}
if (!restrictedClass.existsProperty(ALLOW_ALL_FIELD))
restrictedClass.createProperty(ALLOW_ALL_FIELD, OType.LINKSET, database.getMetadata().getSchema().getClass(OIdentity.CLASS_NAME), unsafe);
if (!restrictedClass.existsProperty(ALLOW_READ_FIELD))
restrictedClass.createProperty(ALLOW_READ_FIELD, OType.LINKSET, database.getMetadata().getSchema().getClass(OIdentity.CLASS_NAME), unsafe);
if (!restrictedClass.existsProperty(ALLOW_UPDATE_FIELD))
restrictedClass.createProperty(ALLOW_UPDATE_FIELD, OType.LINKSET, database.getMetadata().getSchema().getClass(OIdentity.CLASS_NAME), unsafe);
if (!restrictedClass.existsProperty(ALLOW_DELETE_FIELD))
restrictedClass.createProperty(ALLOW_DELETE_FIELD, OType.LINKSET, database.getMetadata().getSchema().getClass(OIdentity.CLASS_NAME), unsafe);
}
use of com.orientechnologies.orient.core.metadata.schema.OClass in project orientdb by orientechnologies.
the class OSecurityShared method load.
public void load() {
final OClass userClass = getDatabase().getMetadata().getSchema().getClass("OUser");
if (userClass != null) {
// @COMPATIBILITY <1.3.0
if (!userClass.existsProperty("status")) {
userClass.createProperty("status", OType.STRING).setMandatory(true).setNotNull(true);
}
OProperty p = userClass.getProperty("name");
if (p == null)
p = userClass.createProperty("name", OType.STRING).setMandatory(true).setNotNull(true).setMin("1").setRegexp("\\S+(.*\\S+)*");
if (userClass.getInvolvedIndexes("name") == null)
p.createIndex(INDEX_TYPE.UNIQUE);
// ROLE
final OClass roleClass = getDatabase().getMetadata().getSchema().getClass("ORole");
final OProperty rules = roleClass.getProperty("rules");
if (rules != null && !OType.EMBEDDEDMAP.equals(rules.getType())) {
roleClass.dropProperty("rules");
}
if (!roleClass.existsProperty("inheritedRole")) {
roleClass.createProperty("inheritedRole", OType.LINK, roleClass);
}
p = roleClass.getProperty("name");
if (p == null)
p = roleClass.createProperty("name", OType.STRING).setMandatory(true).setNotNull(true);
if (roleClass.getInvolvedIndexes("name") == null)
p.createIndex(INDEX_TYPE.UNIQUE);
}
}
use of com.orientechnologies.orient.core.metadata.schema.OClass in project orientdb by orientechnologies.
the class ODocumentFieldWalker method walkDocument.
private void walkDocument(ODocument document, ODocumentFieldVisitor fieldWalker, Set<ODocument> walked) {
if (walked.contains(document))
return;
walked.add(document);
boolean oldLazyLoad = document.isLazyLoad();
document.setLazyLoad(false);
final boolean updateMode = fieldWalker.updateMode();
final OClass clazz = ODocumentInternal.getImmutableSchemaClass(document);
for (String fieldName : document.fieldNames()) {
final OType concreteType = document.fieldType(fieldName);
OType fieldType = concreteType;
OType linkedType = null;
if (fieldType == null && clazz != null) {
OProperty property = clazz.getProperty(fieldName);
if (property != null) {
fieldType = property.getType();
linkedType = property.getLinkedType();
}
}
Object fieldValue = document.field(fieldName, fieldType);
Object newValue = fieldWalker.visitField(fieldType, linkedType, fieldValue);
boolean updated;
if (updateMode)
updated = updateFieldValueIfChanged(document, fieldName, fieldValue, newValue, concreteType);
else
updated = false;
// 3. document is not not embedded.
if (!updated && fieldValue != null && !(OType.LINK.equals(fieldType) || OType.LINKBAG.equals(fieldType) || OType.LINKLIST.equals(fieldType) || OType.LINKSET.equals(fieldType) || (fieldValue instanceof ORecordLazyMultiValue))) {
if (fieldWalker.goDeeper(fieldType, linkedType, fieldValue)) {
if (fieldValue instanceof Map)
walkMap((Map) fieldValue, fieldType, fieldWalker, walked);
else if (fieldValue instanceof ODocument) {
final ODocument doc = (ODocument) fieldValue;
if (OType.EMBEDDED.equals(fieldType) || doc.isEmbedded())
walkDocument((ODocument) fieldValue, fieldWalker);
} else if (OMultiValue.isIterable(fieldValue))
walkIterable(OMultiValue.getMultiValueIterable(fieldValue), fieldType, fieldWalker, walked);
}
}
if (!fieldWalker.goFurther(fieldType, linkedType, fieldValue, newValue)) {
document.setLazyLoad(oldLazyLoad);
return;
}
}
document.setLazyLoad(oldLazyLoad);
}
Aggregations