Search in sources :

Example 1 with UserManager

use of org.opennms.netmgt.config.UserManager in project opennms by OpenNMS.

the class DefaultReportWrapperService method getDeliveryOptions.

/**
 * {@inheritDoc}
 */
@Override
public DeliveryOptions getDeliveryOptions(final String reportId, final String userId) {
    final DeliveryOptions options = new DeliveryOptions();
    options.setFormat(ReportFormat.HTML);
    options.setPersist(true);
    options.setSendMail(false);
    Logging.withPrefix(LOG4J_CATEGORY, new Runnable() {

        @Override
        public void run() {
            UserManager userFactory = UserFactory.getInstance();
            try {
                final String emailAddress = userFactory.getEmail(userId);
                if (emailAddress != null && !emailAddress.isEmpty()) {
                    options.setMailTo(emailAddress);
                }
            } catch (final Exception e) {
                LOG.error("An error occurred while attempting to determine and set the destination email address for user {}", userId, e);
            }
            options.setInstanceId(reportId + " " + userId);
        }
    });
    return options;
}
Also used : UserManager(org.opennms.netmgt.config.UserManager) DeliveryOptions(org.opennms.reporting.core.DeliveryOptions) ReportException(org.opennms.api.reporting.ReportException) JavaMailerException(org.opennms.javamail.JavaMailerException)

Example 2 with UserManager

use of org.opennms.netmgt.config.UserManager in project opennms by OpenNMS.

the class DeleteUserServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String userID = request.getParameter("userID");
    // now save to the xml file
    try {
        UserFactory.init();
        UserManager userFactory = UserFactory.getInstance();
        userFactory.deleteUser(userID);
    } catch (Throwable e) {
        throw new ServletException("Error deleting user " + userID, e);
    }
    response.sendRedirect("list.jsp");
}
Also used : ServletException(javax.servlet.ServletException) UserManager(org.opennms.netmgt.config.UserManager)

Example 3 with UserManager

use of org.opennms.netmgt.config.UserManager in project opennms by OpenNMS.

the class ModifyUserServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession userSession = request.getSession(true);
    try {
        UserFactory.init();
        UserManager userFactory = UserFactory.getInstance();
        User user = userFactory.getUser(request.getParameter("userID"));
        userSession.setAttribute("user.modifyUser.jsp", user);
    } catch (Throwable e) {
        throw new ServletException("Couldn't initialize UserFactory", e);
    }
    // forward the request for proper display
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/userGroupView/users/modifyUser.jsp");
    dispatcher.forward(request, response);
}
Also used : ServletException(javax.servlet.ServletException) User(org.opennms.netmgt.config.users.User) HttpSession(javax.servlet.http.HttpSession) UserManager(org.opennms.netmgt.config.UserManager) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 4 with UserManager

use of org.opennms.netmgt.config.UserManager in project opennms by OpenNMS.

the class NewPasswordActionServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        UserFactory.init();
    } catch (Throwable e) {
        throw new ServletException("NewPasswordActionServlet: Error initialising user factory." + e);
    }
    HttpSession userSession = request.getSession(false);
    UserManager userFactory = UserFactory.getInstance();
    User user = (User) userSession.getAttribute("user.newPassword.jsp");
    String currentPassword = request.getParameter("currentPassword");
    String newPassword = request.getParameter("newPassword");
    if (!request.isUserInRole(Authentication.ROLE_ADMIN) && user.getRoles().contains(Authentication.ROLE_READONLY)) {
        throw new ServletException("User " + user.getUserId() + " is read-only");
    }
    if (!userFactory.comparePasswords(user.getUserId(), currentPassword)) {
        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/account/selfService/newPassword.jsp?action=redo");
        dispatcher.forward(request, response);
    } else {
        final Password pass = new Password();
        pass.setEncryptedPassword(userFactory.encryptedPassword(newPassword, true));
        pass.setSalt(true);
        user.setPassword(pass);
        userSession.setAttribute("user.newPassword.jsp", user);
        try {
            userFactory.saveUser(user.getUserId(), user);
        } catch (Throwable e) {
            throw new ServletException("Error saving user " + user.getUserId(), e);
        }
        // forward the request for proper display
        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/account/selfService/passwordChanged.jsp");
        dispatcher.forward(request, response);
    }
}
Also used : ServletException(javax.servlet.ServletException) User(org.opennms.netmgt.config.users.User) HttpSession(javax.servlet.http.HttpSession) UserManager(org.opennms.netmgt.config.UserManager) RequestDispatcher(javax.servlet.RequestDispatcher) Password(org.opennms.netmgt.config.users.Password)

Example 5 with UserManager

use of org.opennms.netmgt.config.UserManager in project opennms by OpenNMS.

the class MagicUsersMigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    if (!canRun()) {
        log("Error: ignoring the execution of the task because the file magic-users.properties was not found. Maybe the task was already successfully executed before.\n");
        return;
    }
    // Parse read-only attributes
    final List<String> readOnlyUsers = new ArrayList<>();
    try {
        boolean readOnly = false;
        for (String line : Files.readAllLines(usersFile.toPath())) {
            if (line.contains("read-only")) {
                Matcher m = Pattern.compile("read-only=\"(.+)\"").matcher(line);
                if (m.find()) {
                    readOnly = Boolean.parseBoolean(m.group(1));
                }
            }
            if (line.contains("user-id")) {
                if (readOnly) {
                    Matcher m = Pattern.compile("user-id[>](.+)[<][/]user-id").matcher(line);
                    if (m.find()) {
                        log("Warning: User %s has read-only flag\n", m.group(1));
                        readOnlyUsers.add(m.group(1));
                    }
                }
                readOnly = false;
            }
        }
        if (!readOnlyUsers.isEmpty()) {
            log("Removing the read-only flags from users.xml\n");
            String content = new String(Files.readAllBytes(usersFile.toPath()), StandardCharsets.UTF_8);
            content = content.replaceAll(" read-only=\".+\"", "");
            Files.write(usersFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix configuration because " + e.getMessage(), e);
    }
    log("Moving security roles into users.xml...\n");
    try {
        UserFactory.init();
        UserManager userManager = UserFactory.getInstance();
        // Retrieve all the currently configured users.
        final List<OnmsUser> users = new ArrayList<>();
        for (final String userName : userManager.getUserNames()) {
            log("Loading configured user: %s...\n", userName);
            users.add(userManager.getOnmsUser(userName));
        }
        // Parse magic-users.properties
        Properties properties = new Properties();
        if (magicUsersFile.exists()) {
            properties.load(new FileInputStream(magicUsersFile));
        } else if (magicUsersFileRPM.exists()) {
            properties.load(new FileInputStream(magicUsersFileRPM));
        } else if (magicUsersFileDEB.exists()) {
            properties.load(new FileInputStream(magicUsersFileDEB));
        } else {
            throw new IllegalArgumentException("Can't find magic-users.properties, or any RPM/DEB backup of it");
        }
        // Look up for custom users and their passwords
        String[] configuredUsers = BundleLists.parseBundleList(properties.getProperty("users"));
        for (String user : configuredUsers) {
            String username = properties.getProperty("user." + user + ".username");
            String password = properties.getProperty("user." + user + ".password");
            OnmsUser newUser = new OnmsUser();
            newUser.setUsername(username);
            newUser.setFullName(user);
            newUser.setComments("This is a system user, do not delete");
            newUser.setPassword(userManager.encryptedPassword(password, true));
            newUser.setPasswordSalted(true);
            users.add(0, newUser);
        }
        // Configure security roles
        String[] configuredRoles = BundleLists.parseBundleList(properties.getProperty("roles"));
        for (final String role : configuredRoles) {
            String userList = properties.getProperty("role." + role + ".users");
            if (userList == null) {
                log("Warning: Role configuration for '%s' does not have 'users' parameter.  Expecting a 'role.%s.users' property. The role will not be usable.\n", role, role);
                continue;
            }
            String[] authUsers = BundleLists.parseBundleList(userList);
            boolean notInDefaultGroup = "true".equals(properties.getProperty("role." + role + ".notInDefaultGroup"));
            String securityRole = "ROLE_" + role.toUpperCase();
            List<String> customRoles = new ArrayList<>();
            for (final String username : authUsers) {
                OnmsUser onmsUser = getUser(users, username);
                if (onmsUser == null) {
                    log("Warning: User %s doesn't exist on users.xml, Ignoring.\n", username);
                } else {
                    addRole(onmsUser, securityRole);
                    if (!notInDefaultGroup && !securityRole.equals(Authentication.ROLE_ADMIN)) {
                        addRole(onmsUser, Authentication.ROLE_USER);
                    }
                    if (!Authentication.isValidRole(securityRole)) {
                        log("Warning: %s is a custom role.\n", securityRole);
                        customRoles.add(role);
                    }
                }
            }
            if (!customRoles.isEmpty()) {
                String roleList = StringUtils.join(customRoles, ',');
                log("Creating %s with roles: %s\n", Authentication.ROLE_CONFIGURATION_FILE, roleList);
                Properties p = new Properties();
                p.put("roles", roleList);
                File configFile = new File(ConfigFileConstants.getHome(), "etc" + File.separator + Authentication.ROLE_CONFIGURATION_FILE);
                p.store(new FileWriter(configFile), "Custom Roles");
            }
        }
        // Update users.xml
        for (final OnmsUser user : users) {
            if (readOnlyUsers.contains(user.getUsername())) {
                addRole(user, Authentication.ROLE_READONLY);
                if (!user.getRoles().contains(Authentication.ROLE_USER)) {
                    addRole(user, Authentication.ROLE_USER);
                }
            }
            userManager.save(user);
        }
    } catch (Throwable e) {
        throw new OnmsUpgradeException("Can't fix configuration because " + e.getMessage(), e);
    }
}
Also used : Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Properties(java.util.Properties) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) FileInputStream(java.io.FileInputStream) UserManager(org.opennms.netmgt.config.UserManager) OnmsUser(org.opennms.netmgt.model.OnmsUser) File(java.io.File)

Aggregations

UserManager (org.opennms.netmgt.config.UserManager)9 ServletException (javax.servlet.ServletException)7 RequestDispatcher (javax.servlet.RequestDispatcher)5 HttpSession (javax.servlet.http.HttpSession)5 User (org.opennms.netmgt.config.users.User)5 Password (org.opennms.netmgt.config.users.Password)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 ReportException (org.opennms.api.reporting.ReportException)1 JavaMailerException (org.opennms.javamail.JavaMailerException)1 OnmsUser (org.opennms.netmgt.model.OnmsUser)1 DeliveryOptions (org.opennms.reporting.core.DeliveryOptions)1 OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)1