Search in sources :

Example 11 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class ConfigManager method savePermissions.

/**
     * Saves permissions settings from web interface.
     *
     * We validate all of the groups before actually adding them.
     *
     * @param transportName Name of the transport to have it's options saved (type of transport)
     * @param overallSetting The general "all(1), some(2), or none(3)" setting for the permissions.
     * @param users List of specific users that have access.
     * @param groups List of specific groups that have access.
     * @param strict Strict permissions associted with setting.
     * @return List of usernames and groups (@ preceded) that were rejected.
     */
public List<String> savePermissions(String transportName, Integer overallSetting, List<String> users, List<String> groups, Boolean strict) {
    JiveGlobals.setProperty("plugin.gateway." + transportName + ".registration", overallSetting.toString());
    JiveGlobals.setProperty("plugin.gateway." + transportName + ".registrationstrict", strict ? "true" : "false");
    PermissionManager permissionManager = new PermissionManager(TransportType.valueOf(transportName));
    List<String> errorList = new ArrayList<String>();
    ArrayList<User> userList = new ArrayList<User>();
    UserManager userManager = UserManager.getInstance();
    for (String username : users) {
        if (username.matches("\\s*")) {
            continue;
        }
        try {
            if (username.contains("@")) {
                JID jid = new JID(username);
                if (!jid.getDomain().equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
                    throw new UserNotFoundException();
                }
                username = username.substring(0, username.indexOf("@"));
            }
            User user = userManager.getUser(username);
            if (user == null || user.getUsername() == null) {
                throw new UserNotFoundException();
            }
            userList.add(user);
        } catch (UserNotFoundException e) {
            Log.warn("User " + username + " not found while adding access rules.");
            errorList.add(username);
        }
    }
    permissionManager.storeUserList(userList);
    ArrayList<Group> groupList = new ArrayList<Group>();
    GroupManager groupManager = GroupManager.getInstance();
    for (String grpname : groups) {
        if (grpname.matches("\\s*")) {
            continue;
        }
        try {
            Group group = groupManager.getGroup(grpname);
            if (group == null || group.getName() == null) {
                throw new GroupNotFoundException();
            }
            groupList.add(group);
        } catch (GroupNotFoundException e) {
            Log.warn("Group " + grpname + " not found while adding access rules.");
            errorList.add("@" + grpname);
        }
    }
    permissionManager.storeGroupList(groupList);
    return errorList;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Group(org.jivesoftware.openfire.group.Group) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) PermissionManager(net.sf.kraken.permissions.PermissionManager) ArrayList(java.util.ArrayList) GroupManager(org.jivesoftware.openfire.group.GroupManager) UserManager(org.jivesoftware.openfire.user.UserManager) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 12 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedController method changeName.

/**
	 * Change name.
	 *
	 * @param currentUserName
	 *            the current user name
	 * @param newUserName
	 *            the new user name
	 * @param deleteOldUser
	 *            the delete old user
	 * @param newEmail
	 *            the new email
	 * @param newRealName
	 *            the new real name
	 * @return true, if successful
	 * @throws ServiceException
	 *             the service exception
	 */
public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser, String newEmail, String newRealName) throws ServiceException {
    UserManager userManager = UserManager.getInstance();
    try {
        User currentUser = userManager.getUser(currentUserName);
        // Old user found, create new one
        String password = AuthFactory.getPassword(currentUserName);
        String newName = (newRealName == null || newRealName.length() == 0) ? currentUser.getName() : newRealName;
        String newMail = (newEmail == null || newEmail.length() == 0) ? currentUser.getEmail() : newEmail;
        User newUser = userManager.createUser(newUserName, password, currentUser.getName(), newMail);
        newUser.setName(newName);
        newUser.setNameVisible(currentUser.isNameVisible());
        newUser.setEmailVisible(currentUser.isEmailVisible());
        newUser.setCreationDate(currentUser.getCreationDate());
        copyRoster(currentUser, newUser, currentUserName);
        copyProperties(currentUser, newUser);
        copyToGroups(currentUserName, newUserName);
        copyVCard(currentUserName, newUserName);
        if (deleteOldUser) {
            deleteUser(currentUser);
        }
    } catch (UserNotFoundException e) {
        throw new ServiceException("Could not find user", currentUserName, ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
    } catch (UserAlreadyExistsException e) {
        throw new ServiceException("Could not create new user", newUserName, ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
    }
    return true;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 13 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedController method addNewUserToOthersRoster.

/**
	 * Adds the new user to others roster.
	 *
	 * @param newUser
	 *            the new user
	 * @param otherItem
	 *            the other item
	 * @param currentUser
	 *            the current user
	 * @throws ServiceException
	 *             the service exception
	 */
private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) throws ServiceException {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                throw new ServiceException("Could not create roster item for user ", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
            } catch (SharedGroupException e) {
                throw new ServiceException("Could not create roster item, because it is a contact from a shared group", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST, e);
            }
        } catch (UserNotFoundException e) {
            throw new ServiceException("Could not create roster item for user " + newUser.getUsername() + "  because it is a contact from a shared group.", newUser.getUsername(), ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Aggregations

UserManager (org.jivesoftware.openfire.user.UserManager)13 User (org.jivesoftware.openfire.user.User)10 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)10 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)7 JID (org.xmpp.packet.JID)5 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Element (org.dom4j.Element)2 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)2 Roster (org.jivesoftware.openfire.roster.Roster)2 RosterItem (org.jivesoftware.openfire.roster.RosterItem)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1