syntax = "proto3";

import "Proto/common/user.proto";
import "Proto/common/target.proto";
import "Proto/common/export.proto";

import "google/protobuf/wrappers.proto";
import "google/protobuf/empty.proto";

package laborato.operator.service;



service OperatorUserControlService {
    rpc CreateUser(CreateUserRequest) returns (CreateUserResponse);
    rpc UpdateUser(UpdateUserRequest) returns (UserControlResponse);
    rpc DeleteUser(UserIdentifier) returns (UserControlResponse);
    rpc EnableUser(EnableUserRequest) returns (UserControlResponse);
    rpc SetUserPassword(SetUserPasswordRequest) returns (UserControlResponse);
    rpc UnlockUser(UserIdentifier) returns (UserControlResponse);
    rpc ExpireUserPassword(UserIdentifier) returns (UserControlResponse);
    rpc SetUserAccountExpiration(SetUserAccountExpirationRequest) returns (UserControlResponse);
    rpc CreateGroup(CreateUserGroupRequest) returns (CreateGroupResponse);
    rpc UpdateGroup(UpdateGroupRequest) returns (UserControlResponse);
    rpc DeleteGroup(GroupRequest) returns (UserControlResponse);
    rpc AddUserToGroup(UserGroupRequest) returns (UserControlResponse);
    rpc RemoveUserFromGroup(UserGroupRequest) returns (UserControlResponse);

    rpc GetUserEffectiveAgents(UserIdRequest) returns (UserAgentsResponse);
    rpc SetGroupChildGroups(SetGroupChildGroupsRequest) returns (UserControlResponse);
    rpc SetUserAgent(SetUserAgentRequest) returns (UserControlResponse);
    rpc SetGroupAgent(SetGroupAgentRequest) returns (UserControlResponse);
    rpc RemoveUserAgent(SetUserAgentRequest) returns (UserControlResponse);
    rpc RemoveGroupAgent(SetGroupAgentRequest) returns (UserControlResponse);

    // USER READ
    rpc GetUser(UserIdRequest) returns (UserResponse);
    rpc GetAllUsers(google.protobuf.Empty) returns (UsersResponse);
    rpc ExportAllUsers(ExportAllUsersRequest) returns (laborato.common.export.ExportResponse);
    rpc GetUserGroups(UserIdRequest) returns (GroupListResponse);
    rpc GetUserAgents(UserIdRequest) returns (UserAgentsResponse);

    // GROUP READ
    rpc GetGroup(GroupIdRequest) returns (GroupResponse);
    rpc GetAllGroups(google.protobuf.Empty) returns (GroupsResponse);
    rpc ExportAllGroup(ExportAllGroupRequest) returns(laborato.common.export.ExportResponse);
    rpc GetGroupTree(google.protobuf.Empty) returns (GroupTreeResponse);

    rpc GetGroupUsers(GroupIdRequest) returns (UserListResponse);
    rpc GetGroupAgents(GroupIdRequest) returns (AgentListResponse);
    rpc GetGroupChildGroups(GroupIdRequest) returns (GroupListResponse);
    rpc GetGroupParentGroups(GroupIdRequest) returns (GroupListResponse);
}
message ExportAllGroupRequest {
    laborato.common.export.ExportFormat export_format = 1;
}
message ExportAllUsersRequest {
   laborato.common.export.ExportFormat export_format = 1;
}

message CreateUserRequest {
    laborato.common.target.Target target = 1;
    laborato.common.user.UserRequest user = 2;
}

message UpdateUserRequest {
    UserIdentifier user = 1;
    laborato.common.user.UserRequest data = 3;
}

message EnableUserRequest {
    UserIdentifier user = 1;
    bool enable = 2;
}

message SetUserPasswordRequest {
    UserIdentifier user = 1;
    string new_password = 2;
}

message SetUserAccountExpirationRequest {
    UserIdentifier user = 1;
    google.protobuf.StringValue account_expiration_date = 2; // ISO 8601
}

message UserIdentifier {
    laborato.common.target.Target target = 1;
    string user_id = 2;
    string sam_id = 3;
}

message UserIdRequest {
    string userId = 1;
}

message GroupIdRequest {
    string groupId = 1;
}

message SetGroupChildGroupsRequest {
    GroupIdRequest group = 1;
    repeated string child_group_ids = 2;
}

message SetUserAgentRequest {
    laborato.common.target.Target target = 1;
    string user_id = 2;
    string password = 3;
    bool password_not_required = 4;
    bool user_cannot_change_password = 5;
    bool smartcard_logon_required = 6;
}

message SetGroupAgentRequest {
    laborato.common.target.Target target = 1;
    string group_id = 2;
}

message UserControlResponse {
    ResponseStatus status = 1;
    optional string error_message = 2;
}

message CreateUserResponse {
    ResponseStatus status = 1;
    optional string error_message = 2;
    optional string user_id = 3;
}

message CreateGroupResponse {
    ResponseStatus status = 1;
    optional string error_message = 2;
    optional string group_id = 3;
}

message UserAgentsResponse {
    ResponseStatus status = 1;
    repeated string agent_ids = 2;
    optional string error_message = 3;
}

message UserResponse {
    ResponseStatus status = 1;
    UserWithIdInfo user = 2;
    optional string error_message = 3;
}

message UsersResponse {
    ResponseStatus status = 1;
    repeated UserWithIdInfo users = 2;
    optional string error_message = 3;
}

message UserWithIdInfo {
    string userId = 1;
    laborato.common.user.UserInfo info  = 2;
}

message GroupResponse {
    ResponseStatus status = 1;
    GroupWithIdInfo group = 2;
    optional string error_message = 3;
}

message GroupsResponse {
    ResponseStatus status = 1;
    repeated GroupWithIdInfo groups = 2;
    optional string error_message = 3;
}

message GroupListResponse {
    ResponseStatus status = 1;
    repeated GroupWithIdInfo groups = 2;
    optional string error_message = 3;
}

message GroupWithIdInfo {
    string groupId = 1;
    laborato.common.user.GroupInfo info = 2;
}

message GroupTreeResponse {
    ResponseStatus status = 1;
    repeated GroupTreeNode roots = 2;
    optional string error_message = 3;
}

message GroupTreeNode {
    string groupId = 1;
    laborato.common.user.GroupInfo info = 2;
    repeated GroupTreeNode children = 3;
}


message UserListResponse {
    ResponseStatus status = 1;
    repeated UserWithIdInfo users = 2;
    optional string error_message = 3;
}

message AgentListResponse {
    ResponseStatus status = 1;
    repeated string agent_ids = 2;
    optional string error_message = 3;
}


enum ResponseStatus {
    RESPONSE_STATUS_OK = 0;
    RESPONSE_STATUS_ERROR = 1;
}

message CreateUserGroupRequest {
    laborato.common.target.Target target = 1;
    string sam_group_name = 2;
    google.protobuf.StringValue description = 3;
    optional string parent_id = 4;
    int32 max_users = 5;
    int32 max_agent = 6;
}

message UpdateGroupRequest {
    laborato.common.target.Target target = 1;
    string group_id = 2;
    string current_sam_group_name = 3;
    string new_sam_group_name = 4;
    google.protobuf.StringValue description = 5;
    int32 max_users = 6;
    int32 max_agent = 7;
}

message GroupRequest {
    laborato.common.target.Target target = 1;
    string sam_group_name = 2;
}

message UserGroupRequest {
    laborato.common.target.Target target = 1;
    string sam_group_name = 2;
    string sam_account_name = 3;
}