use of com.helger.photon.security.user.IUserManager in project phoss-directory by phax.
the class AppSecurity method init.
public static void init() {
final IUserManager aUserMgr = PhotonSecurityManager.getUserMgr();
final IUserGroupManager aUserGroupMgr = PhotonSecurityManager.getUserGroupMgr();
final IRoleManager aRoleMgr = PhotonSecurityManager.getRoleMgr();
// Standard users
if (!aUserMgr.containsWithID(USER_ADMINISTRATOR_ID)) {
final boolean bDisabled = false;
aUserMgr.createPredefinedUser(USER_ADMINISTRATOR_ID, USER_ADMINISTRATOR_LOGINNAME, USER_ADMINISTRATOR_EMAIL, USER_ADMINISTRATOR_PASSWORD, USER_ADMINISTRATOR_FIRSTNAME, USER_ADMINISTRATOR_LASTNAME, USER_ADMINISTRATOR_DESCRIPTION, USER_ADMINISTRATOR_LOCALE, USER_ADMINISTRATOR_CUSTOMATTRS, bDisabled);
}
// Create all roles
if (!aRoleMgr.containsWithID(ROLE_CONFIG_ID))
aRoleMgr.createPredefinedRole(ROLE_CONFIG_ID, ROLE_CONFIG_NAME, ROLE_CONFIG_DESCRIPTION, ROLE_CONFIG_CUSTOMATTRS);
if (!aRoleMgr.containsWithID(ROLE_VIEW_ID))
aRoleMgr.createPredefinedRole(ROLE_VIEW_ID, ROLE_VIEW_NAME, ROLE_VIEW_DESCRIPTION, ROLE_VIEW_CUSTOMATTRS);
if (!aRoleMgr.containsWithID(ROLE_SG_OWNER_ID))
aRoleMgr.createPredefinedRole(ROLE_SG_OWNER_ID, ROLE_SG_OWNER_NAME, ROLE_SG_OWNER_DESCRIPTION, ROLE_SG_OWNER_CUSTOMATTRS);
// User group Administrators
if (!aUserGroupMgr.containsWithID(USERGROUP_ADMINISTRATORS_ID)) {
aUserGroupMgr.createPredefinedUserGroup(USERGROUP_ADMINISTRATORS_ID, USERGROUP_ADMINISTRATORS_NAME, USERGROUP_ADMINISTRATORS_DESCRIPTION, USERGROUP_ADMINISTRATORS_CUSTOMATTRS);
// Assign administrator user to administrators user group
aUserGroupMgr.assignUserToUserGroup(USERGROUP_ADMINISTRATORS_ID, USER_ADMINISTRATOR_ID);
}
aUserGroupMgr.assignRoleToUserGroup(USERGROUP_ADMINISTRATORS_ID, ROLE_CONFIG_ID);
aUserGroupMgr.assignRoleToUserGroup(USERGROUP_ADMINISTRATORS_ID, ROLE_VIEW_ID);
aUserGroupMgr.assignRoleToUserGroup(USERGROUP_ADMINISTRATORS_ID, ROLE_SG_OWNER_ID);
// User group for Config users
if (!aUserGroupMgr.containsWithID(USERGROUP_CONFIG_ID))
aUserGroupMgr.createPredefinedUserGroup(USERGROUP_CONFIG_ID, USERGROUP_CONFIG_NAME, USERGROUP_CONFIG_DESCRIPTION, USERGROUP_CONFIG_CUSTOMATTRS);
aUserGroupMgr.assignRoleToUserGroup(USERGROUP_CONFIG_ID, ROLE_CONFIG_ID);
// User group for View users
if (!aUserGroupMgr.containsWithID(USERGROUP_VIEW_ID))
aUserGroupMgr.createPredefinedUserGroup(USERGROUP_VIEW_ID, USERGROUP_VIEW_NAME, USERGROUP_VIEW_DESCRIPTION, USERGROUP_VIEW_CUSTOMATTRS);
aUserGroupMgr.assignRoleToUserGroup(USERGROUP_VIEW_ID, ROLE_VIEW_ID);
// Allow to kick old sessions
LoggedInUserManager.getInstance().setLogoutAlreadyLoggedInUser(true);
}
use of com.helger.photon.security.user.IUserManager in project phoss-smp by phax.
the class V12__MigrateUserGroupsToDB method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all user groups to the DB");
final String sFilename = PhotonSecurityManager.FactoryXML.DIRECTORY_SECURITY + PhotonSecurityManager.FactoryXML.FILENAME_USERGROUPS_XML;
final File aFile = WebFileIO.getDataIO().getFile(sFilename);
if (aFile.exists()) {
final IUserManager aUserMgr = new UserManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
final IRoleManager aRoleMgr = new RoleManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
final UserGroupManager aMgrXML = new UserGroupManager(sFilename, aUserMgr, aRoleMgr);
final ICommonsList<IUserGroup> aUserGroups = aMgrXML.getAll();
if (aUserGroups.isNotEmpty()) {
final UserGroupManagerJDBC aMgrNew = new UserGroupManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER, aUserMgr, aRoleMgr);
for (final IUserGroup aUserGroup : aUserGroups) {
// Don't run the callback here
if (aMgrNew.internalCreateNewUserGroup((UserGroup) aUserGroup, false, false) == null)
LOGGER.error("Failed to migrate user group " + aUserGroup + " to DB");
}
}
// Rename to avoid later inconsistencies
WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
LOGGER.info("Finished migrating all " + aUserGroups.size() + " user groups to the DB");
} else {
LOGGER.warn("No user group file found");
}
}
}
use of com.helger.photon.security.user.IUserManager in project phoss-smp by phax.
the class SMPSecurity method init.
public static void init() {
LOGGER.info("Starting to initialize SMP security");
final IUserManager aUserMgr = PhotonSecurityManager.getUserMgr();
final IUserGroupManager aUserGroupMgr = PhotonSecurityManager.getUserGroupMgr();
final IRoleManager aRoleMgr = PhotonSecurityManager.getRoleMgr();
// Standard users
if (!aUserMgr.containsWithID(CSMP.USER_ADMINISTRATOR_ID)) {
final boolean bDisabled = false;
aUserMgr.createPredefinedUser(CSMP.USER_ADMINISTRATOR_ID, CSMP.USER_ADMINISTRATOR_LOGINNAME, CSMP.USER_ADMINISTRATOR_EMAIL, CSMP.USER_ADMINISTRATOR_PASSWORD, CSMP.USER_ADMINISTRATOR_FIRSTNAME, CSMP.USER_ADMINISTRATOR_LASTNAME, CSMP.USER_ADMINISTRATOR_DESCRIPTION, CSMP.USER_ADMINISTRATOR_LOCALE, CSMP.USER_ADMINISTRATOR_CUSTOMATTRS, bDisabled);
}
// Create all roles
if (!aRoleMgr.containsWithID(CSMP.ROLE_CONFIG_ID))
aRoleMgr.createPredefinedRole(CSMP.ROLE_CONFIG_ID, CSMP.ROLE_CONFIG_NAME, CSMP.ROLE_CONFIG_DESCRIPTION, CSMP.ROLE_CONFIG_CUSTOMATTRS);
if (!aRoleMgr.containsWithID(CSMP.ROLE_WRITABLERESTAPI_ID))
aRoleMgr.createPredefinedRole(CSMP.ROLE_WRITABLERESTAPI_ID, CSMP.ROLE_WRITABLERESTAPI_NAME, CSMP.ROLE_WRITABLERESTAPI_DESCRIPTION, CSMP.ROLE_WRITABLERESTAPI_CUSTOMATTRS);
// User group Administrators
if (!aUserGroupMgr.containsWithID(CSMP.USERGROUP_ADMINISTRATORS_ID)) {
aUserGroupMgr.createPredefinedUserGroup(CSMP.USERGROUP_ADMINISTRATORS_ID, CSMP.USERGROUP_ADMINISTRATORS_NAME, CSMP.USERGROUP_ADMINISTRATORS_DESCRIPTION, CSMP.USERGROUP_ADMINISTRATORS_CUSTOMATTRS);
// Assign administrator user to administrators user group
aUserGroupMgr.assignUserToUserGroup(CSMP.USERGROUP_ADMINISTRATORS_ID, CSMP.USER_ADMINISTRATOR_ID);
}
aUserGroupMgr.assignRoleToUserGroup(CSMP.USERGROUP_ADMINISTRATORS_ID, CSMP.ROLE_CONFIG_ID);
aUserGroupMgr.assignRoleToUserGroup(CSMP.USERGROUP_ADMINISTRATORS_ID, CSMP.ROLE_WRITABLERESTAPI_ID);
// User group for Config users
if (!aUserGroupMgr.containsWithID(CSMP.USERGROUP_CONFIG_ID))
aUserGroupMgr.createPredefinedUserGroup(CSMP.USERGROUP_CONFIG_ID, CSMP.USERGROUP_CONFIG_NAME, CSMP.USERGROUP_CONFIG_DESCRIPTION, CSMP.USERGROUP_CONFIG_CUSTOMATTRS);
aUserGroupMgr.assignRoleToUserGroup(CSMP.USERGROUP_CONFIG_ID, CSMP.ROLE_CONFIG_ID);
// User group for Writable REST API users
if (!aUserGroupMgr.containsWithID(CSMP.USERGROUP_WRITABLERESTAPI_ID))
aUserGroupMgr.createPredefinedUserGroup(CSMP.USERGROUP_WRITABLERESTAPI_ID, CSMP.USERGROUP_WRITABLERESTAPI_NAME, CSMP.USERGROUP_WRITABLERESTAPI_DESCRIPTION, CSMP.USERGROUP_WRITABLERESTAPI_CUSTOMATTRS);
aUserGroupMgr.assignRoleToUserGroup(CSMP.USERGROUP_WRITABLERESTAPI_ID, CSMP.ROLE_WRITABLERESTAPI_ID);
// New login logs out old user
LoggedInUserManager.getInstance().setLogoutAlreadyLoggedInUser(true);
// Setup internal error handler (if configured)
SMPInternalErrorHandler.doSetup();
LOGGER.info("Finished initializing SMP security");
}
use of com.helger.photon.security.user.IUserManager in project phoss-smp by phax.
the class V15__MigrateDBUsersToPhotonUsers method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all old DB users to ph-oton users");
final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
// Old JDBC user manager
final SMPUserManagerJDBC aSQLUserMgr = new SMPUserManagerJDBC(SMPDBExecutor::new);
final ICommonsList<DBUser> aSQLUsers = aSQLUserMgr.getAllUsers();
LOGGER.info("Found " + aSQLUsers.size() + " DB user to migrate");
final ICommonsOrderedMap<String, String> aCreatedMappings = new CommonsLinkedHashMap<>();
// New JDBC user manager
final IUserManager aPhotonUserMgr = PhotonSecurityManager.getUserMgr();
for (final DBUser aSQLUser : aSQLUsers) {
final DBUser aDBUser = aSQLUser;
IUser aPhotonUser = null;
int nIndex = 0;
while (true) {
final String sUserName = aDBUser.getUserName() + (nIndex > 0 ? Integer.toString(nIndex) : "");
// The suffix "@example.org" is added to make it an email-address
final String sEmailAddress = sUserName + "@example.org";
aPhotonUser = aPhotonUserMgr.createNewUser(sEmailAddress, sEmailAddress, aDBUser.getPassword(), null, sUserName, null, CSMPServer.DEFAULT_LOCALE, null, false);
if (aPhotonUser != null) {
// New user was successfully created
break;
}
// User name already taken
++nIndex;
if (nIndex > 1000) {
// Avoid endless loop
throw new IllegalStateException("Too many iterations mapping the DB user '" + aDBUser.getUserName() + "' to a ph-oton user");
}
}
aCreatedMappings.put(aDBUser.getUserName(), aPhotonUser.getID());
LOGGER.info("Mapped DB user '" + aDBUser.getUserName() + "' to ph-oton user " + aPhotonUser.getID());
}
// Update the ownership in "smp_ownership"
// Remove the table "smp_user"
aSQLUserMgr.updateOwnershipsAndKillUsers(aCreatedMappings);
if (XMLMapHandler.writeMap(aCreatedMappings, new FileSystemResource(WebFileIO.getDataIO().getFile("migrations/db-photon-user-mapping-" + eDBType.getID() + ".xml"))).isFailure())
LOGGER.error("Failed to store mapping of DB users to ph-oton users as XML");
LOGGER.info("Finished migrating all DB users to ph-oton users");
}
}
use of com.helger.photon.security.user.IUserManager in project phoss-smp by phax.
the class PageSecureServiceGroupImport method fillContent.
@Override
protected void fillContent(@Nonnull final WebPageExecutionContext aWPEC) {
final HCNodeList aNodeList = aWPEC.getNodeList();
final Locale aDisplayLocale = aWPEC.getDisplayLocale();
final ISMPSettings aSettings = SMPMetaManager.getSettings();
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPBusinessCardManager aBusinessCardMgr = SMPMetaManager.getBusinessCardMgr();
final IUserManager aUserMgr = PhotonSecurityManager.getUserMgr();
final ICommonsSet<String> aAllServiceGroupIDs = aServiceGroupMgr.getAllSMPServiceGroupIDs();
final ICommonsSet<String> aAllBusinessCardIDs = aBusinessCardMgr.getAllSMPBusinessCardIDs();
final FormErrorList aFormErrors = new FormErrorList();
final HCUL aImportResultUL = new HCUL();
if (aWPEC.hasAction(CPageParam.ACTION_PERFORM)) {
// Start import
final IFileItem aImportFile = aWPEC.params().getAsFileItem(FIELD_IMPORT_FILE);
final boolean bOverwriteExisting = aWPEC.params().isCheckBoxChecked(FIELD_OVERWRITE_EXISTING, DEFAULT_OVERWRITE_EXISTING);
final String sDefaultOwnerID = aWPEC.params().getAsString(FIELD_DEFAULT_OWNER);
final IUser aDefaultOwner = aUserMgr.getActiveUserOfID(sDefaultOwnerID);
if (aImportFile == null || aImportFile.getSize() == 0)
aFormErrors.addFieldError(FIELD_IMPORT_FILE, "A file to import must be selected!");
if (StringHelper.hasNoText(sDefaultOwnerID))
aFormErrors.addFieldError(FIELD_DEFAULT_OWNER, "A default owner must be selected!");
else if (aDefaultOwner == null)
aFormErrors.addFieldError(FIELD_DEFAULT_OWNER, "A valid default owner must be selected!");
if (aFormErrors.isEmpty()) {
final SAXReaderSettings aSRS = new SAXReaderSettings();
final IMicroDocument aDoc = MicroReader.readMicroXML(aImportFile, aSRS);
if (aDoc == null || aDoc.getDocumentElement() == null)
aFormErrors.addFieldError(FIELD_IMPORT_FILE, "The provided file is not a valid XML file!");
else {
// Start interpreting
final String sVersion = aDoc.getDocumentElement().getAttributeValue(CSMPExchange.ATTR_VERSION);
if (CSMPExchange.VERSION_10.equals(sVersion)) {
// Version 1.0
final ICommonsList<ImportActionItem> aActionList = new CommonsArrayList<>();
final ImportSummary aImportSummary = new ImportSummary();
ServiceGroupImport.importXMLVer10(aDoc.getDocumentElement(), bOverwriteExisting, aDefaultOwner, aAllServiceGroupIDs, aAllBusinessCardIDs, aActionList, aImportSummary);
for (final ImportActionItem aAction : aActionList) {
final IErrorLevel aErrorLevel = aAction.getErrorLevel();
final EBootstrapBadgeType eBadgeType;
if (aErrorLevel.isGE(EErrorLevel.ERROR))
eBadgeType = EBootstrapBadgeType.DANGER;
else if (aErrorLevel.isGE(EErrorLevel.WARN))
eBadgeType = EBootstrapBadgeType.WARNING;
else if (aErrorLevel.isGE(EErrorLevel.INFO))
eBadgeType = EBootstrapBadgeType.INFO;
else
eBadgeType = EBootstrapBadgeType.SUCCESS;
// By default is is centered
aImportResultUL.addItem(new BootstrapBadge(eBadgeType).addChild((aAction.hasParticipantID() ? "[" + aAction.getParticipantID() + "] " : "") + aAction.getMessage()).addChild(SMPCommonUI.getTechnicalDetailsUI(aAction.getLinkedException())).addClass(CBootstrapCSS.TEXT_LEFT));
}
} else {
// Unsupported or no version present
if (sVersion == null)
aFormErrors.addFieldError(FIELD_IMPORT_FILE, "The provided file cannot be imported because it has the wrong layout.");
else
aFormErrors.addFieldError(FIELD_IMPORT_FILE, "The provided file contains the unsupported version '" + sVersion + "'.");
}
}
}
}
final boolean bHandleBusinessCards = aSettings.isDirectoryIntegrationEnabled();
if (aImportResultUL.hasChildren()) {
final BootstrapCard aPanel = new BootstrapCard();
aPanel.createAndAddHeader().addChild("Import results");
aPanel.createAndAddBody().addChild(aImportResultUL);
aNodeList.addChild(aPanel);
}
aNodeList.addChild(info("Import service groups incl. all endpoints" + (bHandleBusinessCards ? " and business cards" : "") + " from a file."));
final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormFileUploadSelf(aWPEC));
aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("File to import").setCtrl(new BootstrapFileUpload(FIELD_IMPORT_FILE, aDisplayLocale)).setErrorList(aFormErrors.getListOfField(FIELD_IMPORT_FILE)));
aForm.addFormGroup(new BootstrapFormGroup().setLabel("Overwrite existing elements").setCtrl(new HCCheckBox(new RequestFieldBoolean(FIELD_OVERWRITE_EXISTING, DEFAULT_OVERWRITE_EXISTING))).setHelpText("If this box is checked, all existing endpoints etc. of a service group are deleted and new endpoints are created! If the " + SMPWebAppConfiguration.getDirectoryName() + " integration is enabled, existing business cards contained in the import are also overwritten!").setErrorList(aFormErrors.getListOfField(FIELD_OVERWRITE_EXISTING)));
aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Owner of the new service groups").setCtrl(new HCUserSelect(new RequestField(FIELD_DEFAULT_OWNER), aDisplayLocale)).setHelpText("This owner is only selected, if the owner contained in the import file is unknown.").setErrorList(aFormErrors.getListOfField(FIELD_DEFAULT_OWNER)));
final BootstrapButtonToolbar aToolbar = aForm.addAndReturnChild(getUIHandler().createToolbar(aWPEC));
aToolbar.addHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM);
aToolbar.addChild(new BootstrapSubmitButton().addChild("Import Service Groups").setIcon(EDefaultIcon.ADD));
}
Aggregations