Search in sources :

Example 1 with APIException

use of net.jforum.exceptions.APIException in project jforum2 by rafaelsteil.

the class UserREST method insert.

/**
 * Creates a new user.
 * Required parameters ara "username", "email" and "password".
 */
public void insert() {
    try {
        this.authenticate();
        String username = this.requiredRequestParameter("username");
        String email = this.requiredRequestParameter("email");
        String password = this.requiredRequestParameter("password");
        if (username.length() > SystemGlobals.getIntValue(ConfigKeys.USERNAME_MAX_LENGTH)) {
            throw new APIException(I18n.getMessage("User.usernameTooBig"));
        }
        if (username.indexOf('<') > -1 || username.indexOf('>') > -1) {
            throw new APIException(I18n.getMessage("User.usernameInvalidChars"));
        }
        UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
        if (dao.isUsernameRegistered(username)) {
            throw new APIException(I18n.getMessage("UsernameExists"));
        }
        if (dao.findByEmail(email) != null) {
            throw new APIException(I18n.getMessage("User.emailExists", new Object[] { email }));
        }
        // Ok, time to insert the user
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        int userId = dao.addNew(user);
        this.setTemplateName(TemplateKeys.API_USER_INSERT);
        this.context.put("userId", new Integer(userId));
    } catch (Exception e) {
        this.setTemplateName(TemplateKeys.API_ERROR);
        this.context.put("exception", e);
    }
}
Also used : APIException(net.jforum.exceptions.APIException) User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) APIException(net.jforum.exceptions.APIException)

Aggregations

UserDAO (net.jforum.dao.UserDAO)1 User (net.jforum.entities.User)1 APIException (net.jforum.exceptions.APIException)1