forked from apache/gravitino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#2238] feat(server): Add the operations for the user
This reverts commit d6cef11.
- Loading branch information
Heng Qin
committed
Mar 29, 2024
1 parent
6641d70
commit a74d02c
Showing
9 changed files
with
726 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
common/src/main/java/com/datastrato/gravitino/dto/authorization/UserDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.dto.authorization; | ||
|
||
import com.datastrato.gravitino.Audit; | ||
import com.datastrato.gravitino.authorization.User; | ||
import com.datastrato.gravitino.dto.AuditDTO; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Preconditions; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** Represents a User Data Transfer Object (DTO). */ | ||
public class UserDTO implements User { | ||
|
||
@JsonProperty("name") | ||
private String name; | ||
|
||
|
||
@JsonProperty("audit") | ||
private AuditDTO audit; | ||
|
||
@Nullable | ||
@JsonProperty("roles") | ||
private List<String> roles; | ||
|
||
/** Default constructor for Jackson deserialization. */ | ||
protected UserDTO() {} | ||
|
||
/** | ||
* Creates a new instance of UserDTO. | ||
* | ||
* @param name The name of the User DTO. | ||
* @param audit The audit information of the User DTO. | ||
*/ | ||
protected UserDTO(String name, AuditDTO audit) { | ||
this.name = name; | ||
this.audit = audit; | ||
} | ||
|
||
/** @return The name of the User DTO. */ | ||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
/** | ||
* The roles of the user. A user can have multiple roles. Every role binds several privileges. | ||
* | ||
* @return The roles of the user. | ||
*/ | ||
@Override | ||
public List<String> roles() { | ||
return roles; | ||
} | ||
|
||
/** @return The audit information of the User DTO. */ | ||
@Override | ||
public Audit auditInfo() { | ||
return audit; | ||
} | ||
|
||
/** | ||
* Creates a new Builder for constructing an User DTO. | ||
* | ||
* @return A new Builder instance. | ||
*/ | ||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* Builder class for constructing a UserDTO instance. | ||
* | ||
* @param <S> The type of the builder instance. | ||
*/ | ||
public static class Builder<S extends Builder> { | ||
|
||
/** The name of the user. */ | ||
protected String name; | ||
|
||
/** The roles of the user. */ | ||
protected List<String> roles; | ||
|
||
/** The audit information of the user. */ | ||
protected AuditDTO audit; | ||
|
||
|
||
/** | ||
* Sets the name of the user. | ||
* | ||
* @param name The name of the user. | ||
* @return The builder instance. | ||
*/ | ||
public S withName(String name) { | ||
this.name = name; | ||
return (S) this; | ||
} | ||
|
||
/** | ||
* Sets the properties of the user. | ||
* | ||
* @param roles The roles of the user. | ||
* @return The builder instance. | ||
*/ | ||
public S withRoles(List<String> roles) { | ||
this.roles = roles; | ||
return (S) this; | ||
} | ||
|
||
/** | ||
* Sets the audit information of the user. | ||
* | ||
* @param audit The audit information of the user. | ||
* @return The builder instance. | ||
*/ | ||
public S withAudit(AuditDTO audit) { | ||
this.audit = audit; | ||
return (S) this; | ||
} | ||
|
||
/** | ||
* Builds an instance of UserDTO using the builder's properties. | ||
* | ||
* @return An instance of UserDTO. | ||
* @throws IllegalArgumentException If the name or audit are not set. | ||
*/ | ||
public UserDTO build() { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty"); | ||
Preconditions.checkArgument(audit != null, "audit cannot be null"); | ||
return new UserDTO(name, audit); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
common/src/main/java/com/datastrato/gravitino/dto/requests/UserAddRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.dto.requests; | ||
|
||
import com.datastrato.gravitino.rest.RESTRequest; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Preconditions; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import lombok.extern.jackson.Jacksonized; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** Represents a request to create a user. */ | ||
@Getter | ||
@EqualsAndHashCode | ||
@ToString | ||
@Builder | ||
@Jacksonized | ||
public class UserAddRequest implements RESTRequest { | ||
|
||
@JsonProperty("name") | ||
private final String name; | ||
|
||
/** Default constructor for MetalakeCreateRequest. (Used for Jackson deserialization.) */ | ||
public UserAddRequest() { | ||
this(null); | ||
} | ||
|
||
/** | ||
* Creates a new UserCreateRequest. | ||
* | ||
* @param name The name of the user. | ||
*/ | ||
public UserAddRequest(String name) { | ||
super(); | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Validates the {@link UserAddRequest} request. | ||
* | ||
* @throws IllegalArgumentException If the request is invalid, this exception is thrown. | ||
*/ | ||
@Override | ||
public void validate() throws IllegalArgumentException { | ||
Preconditions.checkArgument( | ||
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty"); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
common/src/main/java/com/datastrato/gravitino/dto/responses/UserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.dto.responses; | ||
|
||
import com.datastrato.gravitino.dto.authorization.UserDTO; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Preconditions; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** Represents a response for a user. */ | ||
@Getter | ||
@ToString | ||
@EqualsAndHashCode(callSuper = true) | ||
public class UserResponse extends BaseResponse { | ||
|
||
@JsonProperty("user") | ||
private final UserDTO user; | ||
|
||
/** | ||
* Constructor for UserResponse. | ||
* | ||
* @param user The user data transfer object. | ||
*/ | ||
public UserResponse(UserDTO user) { | ||
super(0); | ||
this.user = user; | ||
} | ||
|
||
/** Default constructor for UserResponse. (Used for Jackson deserialization.) */ | ||
public UserResponse() { | ||
super(); | ||
this.user = null; | ||
} | ||
|
||
/** | ||
* Validates the response data. | ||
* | ||
* @throws IllegalArgumentException if the name or audit is not set. | ||
*/ | ||
@Override | ||
public void validate() throws IllegalArgumentException { | ||
super.validate(); | ||
|
||
Preconditions.checkArgument(user != null, "user must not be null"); | ||
Preconditions.checkArgument( | ||
StringUtils.isNotBlank(user.name()), "user 'name' must not be null and empty"); | ||
Preconditions.checkArgument(user.auditInfo() != null, "user 'auditInfo' must not be null"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.