Search in sources :

Example 1 with User

use of org.opennms.netmgt.config.users.User in project opennms by OpenNMS.

the class UserGroupManagerTest method testSaveUser.

@Test
public void testSaveUser() throws Exception {
    String userName = "brozow";
    User user = brozow;
    Calendar nightCal = Calendar.getInstance();
    nightCal.setTime(night);
    Calendar dayCal = Calendar.getInstance();
    dayCal.setTime(day);
    // initial has no duty schedule so always on duty
    assertTrue(m_userManager.isUserOnDuty(userName, dayCal));
    assertTrue(m_userManager.isUserOnDuty(userName, nightCal));
    brozow.addDutySchedule("MoTuWeThFr0900-1700");
    m_userManager.saveUser(userName, user);
    // now user is on duty only from 9-5
    assertTrue(m_userManager.isUserOnDuty(userName, dayCal));
    assertFalse(m_userManager.isUserOnDuty(userName, nightCal));
}
Also used : User(org.opennms.netmgt.config.users.User) OnmsUser(org.opennms.netmgt.model.OnmsUser) Calendar(java.util.Calendar) Test(org.junit.Test)

Example 2 with User

use of org.opennms.netmgt.config.users.User in project opennms by OpenNMS.

the class LoginModuleUtils method doLogin.

public static boolean doLogin(final OpenNMSLoginHandler handler, final Subject subject, final Map<String, ?> sharedState, final Map<String, ?> options) throws LoginException {
    LOG.debug("OpenNMSLoginModule: login(): handler={}, subject={}, sharedState={}, options={}", handler.getClass(), subject.getClass(), sharedState, options);
    final Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        handler.callbackHandler().handle(callbacks);
    } catch (final IOException ioe) {
        LOG.debug("IO exception while attempting to prompt for username and password.", ioe);
        throw new LoginException(ioe.getMessage());
    } catch (final UnsupportedCallbackException uce) {
        LOG.debug("Username or password prompt not supported.", uce);
        throw new LoginException(uce.getMessage() + " not available to obtain information from user.");
    }
    final String user = ((NameCallback) callbacks[0]).getName();
    handler.setUser(user);
    if (user == null) {
        final String msg = "Username can not be null.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    // password callback get value
    if (((PasswordCallback) callbacks[1]).getPassword() == null) {
        final String msg = "Password can not be null.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    final String password = new String(((PasswordCallback) callbacks[1]).getPassword());
    final User configUser;
    final SpringSecurityUser onmsUser;
    try {
        configUser = handler.userConfig().getUser(user);
        onmsUser = handler.springSecurityUserDao().getByUsername(user);
    } catch (final Exception e) {
        final String message = "Failed to retrieve user " + user + " from OpenNMS UserConfig.";
        LOG.debug(message, e);
        throw new LoginException(message);
    }
    if (configUser == null) {
        final String msg = "User  " + user + " does not exist.";
        LOG.debug(msg);
        throw new FailedLoginException(msg);
    }
    if (!handler.userConfig().comparePasswords(user, password)) {
        final String msg = "Login failed: passwords did not match.";
        LOG.debug(msg);
        throw new FailedLoginException(msg);
    }
    ;
    boolean allowed = true;
    final Set<Principal> principals = LoginModuleUtils.createPrincipals(handler, onmsUser.getAuthorities());
    handler.setPrincipals(principals);
    if (handler.requiresAdminRole()) {
        allowed = false;
        for (final Principal principal : principals) {
            final String name = principal.getName().toLowerCase().replaceAll("^role_", "");
            if ("admin".equals(name)) {
                allowed = true;
            }
        }
    }
    if (!allowed) {
        final String msg = "User " + user + " is not an administrator!  OSGi console access is forbidden.";
        LOG.debug(msg);
        throw new LoginException(msg);
    }
    LOG.debug("Successfully logged in {}.", user);
    return true;
}
Also used : User(org.opennms.netmgt.config.users.User) IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 3 with User

use of org.opennms.netmgt.config.users.User 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 4 with User

use of org.opennms.netmgt.config.users.User in project opennms by OpenNMS.

the class BroadcastEventProcessor method makeEmailTask.

/**
     * 
     */
NotificationTask makeEmailTask(long sendTime, Map<String, String> parameters, int noticeId, String address, String[] commandList, List<NotificationTask> siblings, String autoNotify) throws IOException {
    NotificationTask task = null;
    task = new NotificationTask(getNotificationManager(), getUserManager(), sendTime, parameters, siblings, autoNotify);
    User user = new User();
    user.setUserId(address);
    Contact contact = new Contact();
    contact.setType("email");
    LOG.debug("email address = {}, using contact type {}", address, contact.getType());
    contact.setInfo(address);
    user.addContact(contact);
    Command[] commands = new Command[commandList.length];
    for (int i = 0; i < commandList.length; i++) {
        commands[i] = getNotificationCommandManager().getCommand(commandList[i]);
    }
    task.setUser(user);
    task.setCommands(commands);
    task.setNoticeId(noticeId);
    task.setAutoNotify(autoNotify);
    return task;
}
Also used : User(org.opennms.netmgt.config.users.User) Command(org.opennms.netmgt.config.notificationCommands.Command) Contact(org.opennms.netmgt.config.users.Contact)

Example 5 with User

use of org.opennms.netmgt.config.users.User in project opennms by OpenNMS.

the class UpdateUserServlet method doPost.

/** {@inheritDoc} */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession userSession = request.getSession(false);
    if (userSession != null) {
        User newUser = (User) userSession.getAttribute("user.modifyUser.jsp");
        try {
            UserFactory.init();
        } catch (Throwable e) {
            throw new ServletException("UpdateUserServlet:init Error initialising UserFactory " + e);
        }
        // get the rest of the user information from the form
        newUser.setFullName(request.getParameter("fullName"));
        newUser.setUserComments(request.getParameter("userComments"));
        String password = request.getParameter("password");
        if (password != null && !password.trim().equals("")) {
            final Password pass = new Password();
            pass.setEncryptedPassword(UserFactory.getInstance().encryptedPassword(password, true));
            pass.setSalt(true);
            newUser.setPassword(pass);
        }
        String tuiPin = request.getParameter("tuiPin");
        if (tuiPin != null && !tuiPin.trim().equals("")) {
            newUser.setTuiPin(tuiPin);
        }
        String email = request.getParameter(ContactType.email.toString());
        String pagerEmail = request.getParameter("pemail");
        String xmppAddress = request.getParameter(ContactType.xmppAddress.toString());
        String microblog = request.getParameter(ContactType.microblog.toString());
        String numericPage = request.getParameter("numericalService");
        String numericPin = request.getParameter("numericalPin");
        String textPage = request.getParameter("textService");
        String textPin = request.getParameter("textPin");
        String workPhone = request.getParameter(ContactType.workPhone.toString());
        String mobilePhone = request.getParameter(ContactType.mobilePhone.toString());
        String homePhone = request.getParameter(ContactType.homePhone.toString());
        newUser.clearContacts();
        Contact tmpContact = new Contact();
        tmpContact.setInfo(email);
        tmpContact.setType(ContactType.email.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(pagerEmail);
        tmpContact.setType(ContactType.pagerEmail.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(xmppAddress);
        tmpContact.setType(ContactType.xmppAddress.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(microblog);
        tmpContact.setType(ContactType.microblog.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(numericPin);
        tmpContact.setServiceProvider(numericPage);
        tmpContact.setType(ContactType.numericPage.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(textPin);
        tmpContact.setServiceProvider(textPage);
        tmpContact.setType(ContactType.textPage.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(workPhone);
        tmpContact.setType(ContactType.workPhone.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(mobilePhone);
        tmpContact.setType(ContactType.mobilePhone.toString());
        newUser.addContact(tmpContact);
        tmpContact = new Contact();
        tmpContact.setInfo(homePhone);
        tmpContact.setType(ContactType.homePhone.toString());
        newUser.addContact(tmpContact);
        // build the duty schedule data structure
        List<Boolean> newSchedule = new ArrayList<Boolean>(7);
        ChoiceFormat days = new ChoiceFormat("0#Mo|1#Tu|2#We|3#Th|4#Fr|5#Sa|6#Su");
        Collection<String> dutySchedules = getDutySchedulesForUser(newUser);
        dutySchedules.clear();
        int dutyCount = WebSecurityUtils.safeParseInt(request.getParameter("dutySchedules"));
        for (int duties = 0; duties < dutyCount; duties++) {
            newSchedule.clear();
            String deleteFlag = request.getParameter("deleteDuty" + duties);
            // don't save any duties that were marked for deletion
            if (deleteFlag == null) {
                for (int i = 0; i < 7; i++) {
                    String curDayFlag = request.getParameter("duty" + duties + days.format(i));
                    newSchedule.add(Boolean.valueOf(curDayFlag != null));
                }
                int startTime = WebSecurityUtils.safeParseInt(request.getParameter("duty" + duties + "Begin"));
                int stopTime = WebSecurityUtils.safeParseInt(request.getParameter("duty" + duties + "End"));
                DutySchedule newDuty = new DutySchedule(newSchedule, startTime, stopTime);
                dutySchedules.add(newDuty.toString());
            }
        }
        // The new list of roles will override the existing one.
        // If the new list is empty or null, that means the user should not have roles, and the existing ones should be removed.
        newUser.getRoles().clear();
        String[] configuredRoles = request.getParameterValues("configuredRoles");
        if (configuredRoles != null && configuredRoles.length > 0) {
            newUser.getRoles().clear();
            for (String role : configuredRoles) {
                newUser.addRole(role);
            }
        }
        userSession.setAttribute("user.modifyUser.jsp", newUser);
    }
    // forward the request for proper display
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(request.getParameter("redirect"));
    dispatcher.forward(request, response);
}
Also used : User(org.opennms.netmgt.config.users.User) HttpSession(javax.servlet.http.HttpSession) ChoiceFormat(java.text.ChoiceFormat) ArrayList(java.util.ArrayList) RequestDispatcher(javax.servlet.RequestDispatcher) Contact(org.opennms.netmgt.config.users.Contact) ServletException(javax.servlet.ServletException) DutySchedule(org.opennms.netmgt.config.users.DutySchedule) Password(org.opennms.netmgt.config.users.Password)

Aggregations

User (org.opennms.netmgt.config.users.User)34 OnmsUser (org.opennms.netmgt.model.OnmsUser)21 RequestDispatcher (javax.servlet.RequestDispatcher)7 HttpSession (javax.servlet.http.HttpSession)7 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 Password (org.opennms.netmgt.config.users.Password)6 Test (org.junit.Test)5 UserManager (org.opennms.netmgt.config.UserManager)5 Contact (org.opennms.netmgt.config.users.Contact)3 DutySchedule (org.opennms.netmgt.config.users.DutySchedule)3 Userinfo (org.opennms.netmgt.config.users.Userinfo)3 IOException (java.io.IOException)2 Command (org.opennms.netmgt.config.notificationCommands.Command)2 OnmsUserList (org.opennms.netmgt.model.OnmsUserList)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1