[DEV] update back service to manage user creation
This commit is contained in:
parent
80a9806bf4
commit
d139ecfefd
@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.kar</groupId>
|
<groupId>org.kar</groupId>
|
||||||
<artifactId>karso</artifactId>
|
<artifactId>karso</artifactId>
|
||||||
<version>0.3.0</version>
|
<version>0.4.0</version>
|
||||||
<properties>
|
<properties>
|
||||||
<!--
|
<!--
|
||||||
<jaxb.version>2.3.1</jaxb.version>
|
<jaxb.version>2.3.1</jaxb.version>
|
||||||
@ -27,7 +27,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>kangaroo-and-rabbit</groupId>
|
<groupId>kangaroo-and-rabbit</groupId>
|
||||||
<artifactId>archidata</artifactId>
|
<artifactId>archidata</artifactId>
|
||||||
<version>0.2.3</version>
|
<version>0.2.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -4,6 +4,8 @@ import org.kar.archidata.model.GetToken;
|
|||||||
import org.kar.archidata.model.User;
|
import org.kar.archidata.model.User;
|
||||||
import org.kar.archidata.SqlWrapper;
|
import org.kar.archidata.SqlWrapper;
|
||||||
import org.kar.archidata.WhereCondition;
|
import org.kar.archidata.WhereCondition;
|
||||||
|
import org.kar.archidata.annotation.security.PermitAll;
|
||||||
|
import org.kar.archidata.annotation.security.RolesAllowed;
|
||||||
import org.kar.archidata.exception.FailException;
|
import org.kar.archidata.exception.FailException;
|
||||||
import org.kar.archidata.exception.InputException;
|
import org.kar.archidata.exception.InputException;
|
||||||
import org.kar.archidata.exception.SystemException;
|
import org.kar.archidata.exception.SystemException;
|
||||||
@ -11,9 +13,6 @@ import org.kar.archidata.filter.GenericContext;
|
|||||||
import org.kar.karso.model.*;
|
import org.kar.karso.model.*;
|
||||||
import org.kar.karso.util.ConfigVariable;
|
import org.kar.karso.util.ConfigVariable;
|
||||||
import org.kar.archidata.util.JWTWrapper;
|
import org.kar.archidata.util.JWTWrapper;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.security.PermitAll;
|
|
||||||
import org.kar.archidata.annotation.security.RolesAllowed;
|
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
@ -24,6 +23,8 @@ import java.util.List;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
@Path("/users")
|
@Path("/users")
|
||||||
@ -47,30 +48,81 @@ public class UserResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
@Path("{id}/set_admin")
|
||||||
@RolesAllowed("ADMIN")
|
@RolesAllowed("ADMIN")
|
||||||
public Response createUser(UserAuth user) {
|
public Response setAdmin(@Context SecurityContext sc, @PathParam("id") long userId, boolean data) throws Exception {
|
||||||
System.out.println("getUser " + user);
|
UserAuth user = new UserAuth();
|
||||||
/*
|
user.admin = data;
|
||||||
DBEntry entry = new DBEntry(WebLauncher.dbConfig);
|
int ret = SqlWrapper.update(user, userId, List.of("admin"));
|
||||||
String query = "SELECT * FROM user WHERE id = ?";
|
if (ret == 0) {
|
||||||
try {
|
return Response.notModified("{}").build();
|
||||||
PreparedStatement ps = entry.connection.prepareStatement(query);
|
|
||||||
ps.setLong(1, userId);
|
|
||||||
ResultSet rs = ps.executeQuery();
|
|
||||||
if (rs.next()) {
|
|
||||||
User out = new User(rs);
|
|
||||||
entry.disconnect();
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
} catch (SQLException throwables) {
|
|
||||||
throwables.printStackTrace();
|
|
||||||
}
|
}
|
||||||
entry.disconnect();
|
return Response.ok("{}").build();
|
||||||
entry = null;
|
}
|
||||||
return null;
|
|
||||||
*/
|
@POST
|
||||||
String result = "User saved ... : " + user;
|
@Path("{id}/set_blocked")
|
||||||
return Response.status(201).entity(result).build();
|
@RolesAllowed("ADMIN")
|
||||||
|
public Response setBlocked(@Context SecurityContext sc, @PathParam("id") long userId, boolean data) throws Exception {
|
||||||
|
UserAuth user = new UserAuth();
|
||||||
|
user.blocked = data;
|
||||||
|
int ret = SqlWrapper.update(user, userId, List.of("blocked"));
|
||||||
|
if (ret == 0) {
|
||||||
|
return Response.notModified("{}").build();
|
||||||
|
}
|
||||||
|
return Response.ok("{}").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("create_new_user")
|
||||||
|
@RolesAllowed("ADMIN")
|
||||||
|
public UserAuthGet createUser(UserCreate user) throws Exception {
|
||||||
|
System.out.println("create new User email=" + user.email + " login=" + user.login);
|
||||||
|
// verify login or email is correct:
|
||||||
|
if (user.login == null || user.login.length() < 6) {
|
||||||
|
throw new InputException("login", "Authentiocate-method-error (login too small: '" + user.login + "')");
|
||||||
|
}
|
||||||
|
// TODO: check login format
|
||||||
|
|
||||||
|
if (user.email == null || user.email.length() < 6) {
|
||||||
|
throw new InputException("email", "Authentiocate-method-error (email too small: '" + user.email + "')");
|
||||||
|
}
|
||||||
|
// TODO: check email format
|
||||||
|
|
||||||
|
if(user.password == null || user.password.length() != 128) {
|
||||||
|
throw new InputException("password", "null password, or wrong hash size");
|
||||||
|
}
|
||||||
|
// TODO: verify if the data are a hash ...
|
||||||
|
|
||||||
|
|
||||||
|
// Check login does not exist
|
||||||
|
List<UserAuth> out = SqlWrapper.getsWhere(UserAuth.class, List.of(
|
||||||
|
new WhereCondition("login", "=", user.login)
|
||||||
|
), false);
|
||||||
|
if (out.size() >= 1) {
|
||||||
|
throw new FailException(Response.Status.BAD_REQUEST, "Login already used !!!");
|
||||||
|
}
|
||||||
|
// Check email does not exist
|
||||||
|
out = SqlWrapper.getsWhere(UserAuth.class, List.of(
|
||||||
|
new WhereCondition("email", "=", user.email)
|
||||||
|
), false);
|
||||||
|
if (out.size() >= 1) {
|
||||||
|
throw new FailException(Response.Status.BAD_REQUEST, "e-mail already used !!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new user and return formated dat.
|
||||||
|
UserAuth newUser = new UserAuth();
|
||||||
|
newUser.admin = false;
|
||||||
|
newUser.removed = false;
|
||||||
|
newUser.blocked = false;
|
||||||
|
newUser.avatar = false;
|
||||||
|
newUser.login = user.login;
|
||||||
|
newUser.password = user.password;
|
||||||
|
newUser.email = user.email;
|
||||||
|
newUser.lastConnection = Timestamp.valueOf(LocalDateTime.now());
|
||||||
|
UserAuth tmp = SqlWrapper.insert(newUser);
|
||||||
|
System.out.println("create new user done with id==" + tmp.id);
|
||||||
|
return SqlWrapper.get(UserAuthGet.class, tmp.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@ -200,6 +252,12 @@ public class UserResource {
|
|||||||
UserAuth user = checkAuthUser(data.method, data.login, data.time, data.password);
|
UserAuth user = checkAuthUser(data.method, data.login, data.time, data.password);
|
||||||
int expirationTimeInMinutes = ConfigVariable.getAuthExpirationTime();
|
int expirationTimeInMinutes = ConfigVariable.getAuthExpirationTime();
|
||||||
String ret = JWTWrapper.generateJWToken(user.id, user.login, "KarAuth", "sso", expirationTimeInMinutes);
|
String ret = JWTWrapper.generateJWToken(user.id, user.login, "KarAuth", "sso", expirationTimeInMinutes);
|
||||||
|
|
||||||
|
// Update last connection:
|
||||||
|
UserAuth newUser = new UserAuth();
|
||||||
|
newUser.lastConnection = Timestamp.valueOf(LocalDateTime.now());
|
||||||
|
SqlWrapper.update(newUser, user.id, List.of("lastConnection"));
|
||||||
|
|
||||||
//System.out.println(" ==> generate token: " + ret);
|
//System.out.println(" ==> generate token: " + ret);
|
||||||
return new GetToken(ret);
|
return new GetToken(ret);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.kar.karso.model;
|
package org.kar.karso.model;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
|
||||||
|
|
||||||
import org.kar.archidata.annotation.SQLDefault;
|
import org.kar.archidata.annotation.SQLDefault;
|
||||||
import org.kar.archidata.annotation.SQLIfNotExists;
|
import org.kar.archidata.annotation.SQLIfNotExists;
|
||||||
import org.kar.archidata.annotation.SQLLimitSize;
|
import org.kar.archidata.annotation.SQLLimitSize;
|
||||||
|
10
back/src/org/kar/karso/model/UserCreate.java
Normal file
10
back/src/org/kar/karso/model/UserCreate.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package org.kar.karso.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
|
||||||
|
public class UserCreate {
|
||||||
|
public String login;
|
||||||
|
public String email;
|
||||||
|
public String password;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user