use of org.structr.core.entity.Principal in project structr by structr.
the class UiAuthenticator method doLogout.
@Override
public void doLogout(final HttpServletRequest request) {
try {
final Principal user = getUser(request, false);
if (user != null) {
AuthHelper.doLogout(request, user);
}
final HttpSession session = request.getSession(false);
if (session != null) {
SessionHelper.invalidateSession(session);
}
} catch (IllegalStateException | FrameworkException ex) {
logger.warn("Error while logging out user", ex);
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class ExportConsoleCommand method run.
@Override
public void run(final SecurityContext securityContext, final List<String> parameters, final Writable writable) throws FrameworkException, IOException {
final Principal user = securityContext.getUser(false);
if (user != null && user.isAdmin()) {
final DeployCommand cmd = StructrApp.getInstance(securityContext).command(DeployCommand.class);
cmd.setLogBuffer(writable);
cmd.execute(toMap("mode", "export", "target", getParameter(parameters, 1)));
} else {
writable.println("You must be admin user to use this command.");
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class InitConsoleCommand method run.
@Override
public void run(final SecurityContext securityContext, final List<String> parameters, final Writable writable) throws FrameworkException, IOException {
final Principal user = securityContext.getUser(false);
final int paramCount = parameters.size();
if (user != null && user.isAdmin()) {
NodeServiceCommand command = null;
boolean allNodes = true;
boolean allRels = true;
boolean isCreateLabels = false;
boolean isIndex = false;
boolean isIds = false;
boolean hasCommand = false;
boolean hasFor = false;
boolean error = false;
String type = null;
String typeKey = null;
String mode = null;
// parse parameters
for (int i = 1; i < paramCount && !error; i++) {
final String param = getParameter(parameters, i);
switch(param) {
case "index":
if (hasCommand) {
writable.println("Syntax error, too many parameters.");
error = true;
} else {
command = StructrApp.getInstance(securityContext).command(BulkRebuildIndexCommand.class);
command.setLogBuffer(writable);
isIndex = true;
hasCommand = true;
}
break;
case "ids":
if (hasCommand) {
writable.println("Syntax error, too many parameters.");
error = true;
} else {
command = StructrApp.getInstance(securityContext).command(BulkSetUuidCommand.class);
command.setLogBuffer(writable);
isIds = true;
hasCommand = true;
}
break;
case "labels":
if (hasCommand) {
writable.println("Syntax error, too many parameters.");
error = true;
} else {
if ("relsOnly".equals(mode)) {
writable.println("Cannot set labels on relationships.");
error = true;
} else {
command = StructrApp.getInstance(securityContext).command(BulkCreateLabelsCommand.class);
command.setLogBuffer(writable);
isCreateLabels = true;
hasCommand = true;
}
}
break;
case "node":
if (hasCommand) {
if (isIndex) {
writable.println("Index type must be specified before the 'index' keyword.");
error = true;
} else if (isIds) {
writable.println("Entity type must be specified before the 'ids' keyword.");
error = true;
}
} else {
mode = "nodesOnly";
typeKey = "type";
allRels = false;
}
break;
case "rel":
case "relationship":
if (hasCommand) {
if (isIndex) {
writable.println("Index type must be specified before the 'index' keyword.");
error = true;
} else if (isIds) {
writable.println("Entity type must be specified before the 'ids' keyword.");
error = true;
}
} else {
if (isCreateLabels) {
writable.println("Cannot set labels on relationships.");
error = true;
}
mode = "relsOnly";
typeKey = "relType";
allNodes = false;
}
break;
case "for":
if (!hasCommand) {
writable.println("Unknown init mode 'for'.");
error = true;
}
hasFor = true;
break;
default:
// specify node or rel type
if (hasCommand && hasFor) {
// prevent too many parameters from being accepted
if (StringUtils.isNotBlank(type)) {
writable.println("Syntax error, too many parameters.");
error = true;
break;
}
type = param;
// only set type key if not already set, default is "type" not "relType"
if (typeKey == null) {
typeKey = "type";
}
} else {
if (!hasCommand) {
writable.println("Unknown init mode '" + param + "'.");
error = true;
} else {
writable.println("Syntax error, please specify something like 'init node index for User'.");
error = true;
}
}
break;
}
// break early on errors
if (error) {
break;
}
}
if (!error && !hasCommand) {
writable.println("Please specify what to initialize.");
error = true;
}
if (!error && hasCommand && hasFor && StringUtils.isEmpty(type)) {
writable.println("Missing type specification, please specify something like 'init node index for User'.");
error = true;
}
if (!error) {
if (command instanceof MaintenanceCommand) {
final Map<String, Object> data = toMap("mode", mode, typeKey, type);
if (type == null) {
data.put("allNodes", allNodes);
data.put("allRels", allRels);
}
((MaintenanceCommand) command).execute(data);
} else if (command != null) {
writable.println("Cannot execute command '" + command.getClass().getSimpleName() + "', wrong type.");
} else {
writable.println("Cannot execute null command.");
}
}
} else {
writable.println("You must be admin user to use this command.");
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class File method getCurrentWorkingDir.
/**
* Returns the Folder entity for the current working directory,
* or the user's home directory as a fallback.
* @return
*/
static Folder getCurrentWorkingDir(final File thisFile) {
final Principal _owner = thisFile.getProperty(owner);
Folder workingOrHomeDir = null;
if (_owner != null && _owner instanceof User) {
final User user = (User) _owner;
workingOrHomeDir = user.getWorkingDirectory();
if (workingOrHomeDir == null) {
workingOrHomeDir = user.getHomeDirectory();
}
}
return workingOrHomeDir;
}
use of org.structr.core.entity.Principal in project structr by structr.
the class AuthHelper method getPrincipalForPassword.
/**
* Find a {@link Principal} with matching password and given key or name
*
* @param key
* @param value
* @param password
* @return principal
* @throws AuthenticationException
*/
public static Principal getPrincipalForPassword(final PropertyKey<String> key, final String value, final String password) throws AuthenticationException {
String errorMsg = null;
Principal principal = null;
final String superuserName = Settings.SuperUserName.getValue();
final String superUserPwd = Settings.SuperUserPassword.getValue();
if (StringUtils.isEmpty(value)) {
logger.info("Empty value for key {}", key);
errorMsg = STANDARD_ERROR_MSG;
}
if (StringUtils.isEmpty(password)) {
logger.info("Empty password");
errorMsg = STANDARD_ERROR_MSG;
}
if (superuserName.equals(value) && superUserPwd.equals(password)) {
// logger.info("############# Authenticated as superadmin! ############");
principal = new SuperUser();
} else if (errorMsg == null) {
try {
principal = StructrApp.getInstance().nodeQuery(Principal.class).and().or(key, value).or(AbstractNode.name, value).disableSorting().getFirst();
if (principal == null) {
logger.info("No principal found for {} {}", new Object[] { key.dbName(), value });
errorMsg = STANDARD_ERROR_MSG;
} else {
if (principal.isBlocked()) {
logger.info("Principal {} is blocked", principal);
errorMsg = STANDARD_ERROR_MSG;
}
if (StringUtils.isEmpty(password)) {
logger.info("Empty password for principal {}", principal);
errorMsg = "Empty password, should never happen here!";
} else {
// let Principal decide how to check password
if (!principal.isValidPassword(password)) {
errorMsg = STANDARD_ERROR_MSG;
}
}
}
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
if (errorMsg != null) {
throw new AuthenticationException(errorMsg);
}
return principal;
}
Aggregations