Search in sources :

Example 11 with User

use of org.traccar.model.User in project traccar by tananaev.

the class SessionResource method get.

@PermitAll
@GET
public User get(@QueryParam("token") String token) throws SQLException, UnsupportedEncodingException {
    Long userId = (Long) request.getSession().getAttribute(USER_ID_KEY);
    if (userId == null) {
        Cookie[] cookies = request.getCookies();
        String email = null, password = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(USER_COOKIE_KEY)) {
                    byte[] emailBytes = DataConverter.parseBase64(URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
                    email = new String(emailBytes, StandardCharsets.UTF_8);
                } else if (cookie.getName().equals(PASS_COOKIE_KEY)) {
                    byte[] passwordBytes = DataConverter.parseBase64(URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
                    password = new String(passwordBytes, StandardCharsets.UTF_8);
                }
            }
        }
        if (email != null && password != null) {
            User user = Context.getPermissionsManager().login(email, password);
            if (user != null) {
                userId = user.getId();
                request.getSession().setAttribute(USER_ID_KEY, userId);
            }
        } else if (token != null) {
            User user = Context.getUsersManager().getUserByToken(token);
            if (user != null) {
                userId = user.getId();
                request.getSession().setAttribute(USER_ID_KEY, userId);
            }
        }
    }
    if (userId != null) {
        Context.getPermissionsManager().checkUserEnabled(userId);
        return Context.getPermissionsManager().getUser(userId);
    } else {
        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
    }
}
Also used : Cookie(javax.servlet.http.Cookie) User(org.traccar.model.User) WebApplicationException(javax.ws.rs.WebApplicationException) GET(javax.ws.rs.GET) PermitAll(javax.annotation.security.PermitAll)

Example 12 with User

use of org.traccar.model.User in project traccar by tananaev.

the class UserResource method add.

@Override
@PermitAll
@POST
public Response add(User entity) throws SQLException {
    if (!Context.getPermissionsManager().getUserAdmin(getUserId())) {
        Context.getPermissionsManager().checkUserUpdate(getUserId(), new User(), entity);
        if (Context.getPermissionsManager().getUserManager(getUserId())) {
            Context.getPermissionsManager().checkUserLimit(getUserId());
        } else {
            Context.getPermissionsManager().checkRegistration(getUserId());
            entity.setDeviceLimit(Context.getConfig().getInteger("users.defaultDeviceLimit", -1));
            int expirationDays = Context.getConfig().getInteger("users.defaultExpirationDays");
            if (expirationDays > 0) {
                entity.setExpirationTime(new Date(System.currentTimeMillis() + (long) expirationDays * 24 * 3600 * 1000));
            }
        }
    }
    Context.getUsersManager().addItem(entity);
    LogAction.create(getUserId(), entity);
    if (Context.getPermissionsManager().getUserManager(getUserId())) {
        Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true);
        LogAction.link(getUserId(), User.class, getUserId(), ManagedUser.class, entity.getId());
    }
    Context.getUsersManager().refreshUserItems();
    return Response.ok(entity).build();
}
Also used : ManagedUser(org.traccar.model.ManagedUser) User(org.traccar.model.User) Date(java.util.Date) POST(javax.ws.rs.POST) PermitAll(javax.annotation.security.PermitAll)

Aggregations

User (org.traccar.model.User)12 PermitAll (javax.annotation.security.PermitAll)4 WebApplicationException (javax.ws.rs.WebApplicationException)3 Date (java.util.Date)2 POST (javax.ws.rs.POST)2 ManagedUser (org.traccar.model.ManagedUser)2 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 SearchResult (javax.naming.directory.SearchResult)1 Cookie (javax.servlet.http.Cookie)1 GET (javax.ws.rs.GET)1 Response (javax.ws.rs.core.Response)1 SecurityContext (javax.ws.rs.core.SecurityContext)1