package automately.core.data;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import io.jsync.app.core.persistence.IdentifiedDataType;
import io.jsync.json.JsonObject;
import io.jsync.json.impl.Json;
import java.io.IOException;
import java.util.Date;
import static io.jsync.utils.CryptoUtils.calculateHmacSHA1;
import static io.jsync.utils.Token.generateToken;
import static java.util.UUID.randomUUID;
/**
* The User class is a DataType used by JCluster.
* It is used for all operations in the Automately Cluster such as
* file store, job execution, and authentication..
*/
public class User extends IdentifiedDataType {
public String username;
// This value is simply a random key that can be used to identify the user
// without a username and many other things
public String publicKey = calculateHmacSHA1(randomUUID().toString(), randomUUID().toString());
public boolean admin = false;
public boolean enabled = true;
public Date created = new Date();
public Date updated = new Date();
private String token = generateToken().toHex();
@Override
public void writeData(ObjectDataOutput dataOutput) throws IOException {
dataOutput.writeUTF(token);
dataOutput.writeUTF(username);
dataOutput.writeUTF(publicKey);
dataOutput.writeBoolean(admin);
dataOutput.writeBoolean(enabled);
dataOutput.writeObject(created);
dataOutput.writeObject(updated);
}
@Override
public void readData(ObjectDataInput dataInput) throws IOException {
token = dataInput.readUTF();
username = dataInput.readUTF();
publicKey = dataInput.readUTF();
admin = dataInput.readBoolean();
enabled = dataInput.readBoolean();
created = dataInput.readObject();
updated = dataInput.readObject();
}
public String token() {
return token;
}
@Override
public void loadJson(JsonObject json) {
if (json != null) {
this.token = json.getString("token", this.token);
this.admin = json.getBoolean("admin", this.admin);
this.username = json.getString("username", this.username);
this.publicKey = json.getString("publicKey", this.publicKey);
this.enabled = json.getBoolean("enabled", this.enabled);
// THIS IS REQUIRED
if(json.getValue("created") instanceof JsonObject &&
json.getObject("created", new JsonObject()).containsField("$numberLong")){
json.putValue("created", Long.valueOf(json.getObject("created", new JsonObject()).getValue("$numberLong")));
}
this.created = new Date(json.getLong("created", created.getTime()));
// THIS IS REQUIRED
if(json.getValue("updated") instanceof JsonObject &&
json.getObject("updated", new JsonObject()).containsField("$numberLong")){
json.putValue("updated", Long.valueOf(json.getObject("updated", new JsonObject()).getValue("$numberLong")));
}
this.updated = new Date(json.getLong("updated", updated.getTime()));
}
}
@Override
public JsonObject toJson() {
JsonObject ret = new JsonObject(Json.encode(this));
ret.putString("token", token);
ret.putNumber("created", created.getTime());
ret.putNumber("updated", updated.getTime());
ret.removeField("factoryId");
ret.removeField("id");
return ret;
}
@Override
public int getFactoryId() {
return IdentifiedDataTypeFactory.FACTORY_ID;
}
@Override
public int getId() {
return IdentifiedDataTypeFactory.USER_TYPE;
}
}