syntax = "proto3";

package rbac;

service RbacService {
  rpc ListRoles (Empty) returns (ListRolesResponse);
  rpc ListPermissions (Empty) returns (ListPermissionsResponse);
  rpc ListOAuthScopes (Empty) returns (ListOAuthScopesResponse);
  rpc ListOAuthClients (ListOAuthClientsRequest) returns (ListOAuthClientsResponse);
  rpc CreateRole (CreateRoleRequest) returns (Role);
  rpc UpdateRole (UpdateRoleRequest) returns (Role);
  rpc DeleteRole (DeleteRoleRequest) returns (Empty);
  rpc AssignUserRole (AssignUserRoleRequest) returns (AssignUserRoleResponse);
  rpc RemoveUserRole (RemoveUserRoleRequest) returns (Empty);
  rpc AssignUserPermission (AssignUserPermissionRequest) returns (AssignUserPermissionResponse);
  rpc RemoveUserPermission (RemoveUserPermissionRequest) returns (AssignUserPermissionResponse);
  rpc CreateOAuthClient (CreateOAuthClientRequest) returns (OAuthClientDetail);
  rpc UpdateOAuthClient (UpdateOAuthClientRequest) returns (OAuthClientDetail);
  rpc DeleteOAuthClient (DeleteOAuthClientRequest) returns (DeleteOAuthClientResponse);
  rpc RotateOAuthSecret (RotateOAuthSecretRequest) returns (RotateOAuthSecretResponse);
}

message Empty {}

message Permission {
  string id = 1;
  string slug = 2;
  string name = 3;
  optional string description = 4;
}

message Role {
  string id = 1;
  string slug = 2;
  string name = 3;
  optional string description = 4;
  repeated Permission permissions = 5;
  bool isSystem = 6;
  bool isDefault = 7;
}

message OAuthScope {
  string id = 1;
  string slug = 2;
  string name = 3;
  optional string description = 4;
}

message OAuthClient {
  string id = 1;
  string name = 2;
  string clientId = 3;
  repeated string redirectUris = 4;
  repeated string scopes = 5;
  bool isActive = 6;
  string type = 7;
  optional string createdById = 8;
  optional string createdByDisplayName = 9;
  bool canManage = 10;
}

message OAuthClientDetail {
  string id = 1;
  string name = 2;
  string clientId = 3;
  repeated string redirectUris = 4;
  repeated string scopes = 5;
  bool isActive = 6;
  string type = 7;
  optional string clientSecret = 8;
  optional string createdById = 9;
  optional string createdByDisplayName = 10;
  bool canManage = 11;
}

message ListRolesResponse {
  repeated Role roles = 1;
}

message ListPermissionsResponse {
  repeated Permission permissions = 1;
}

message ListOAuthScopesResponse {
  repeated OAuthScope scopes = 1;
}

message ListOAuthClientsResponse {
  repeated OAuthClient clients = 1;
}

message ListOAuthClientsRequest {
  string actorUserId = 1;
}

message CreateRoleRequest {
  string actorUserId = 1;
  string slug = 2;
  string name = 3;
  optional string description = 4;
  repeated string permissionSlugs = 5;
}

message UpdateRoleRequest {
  string actorUserId = 1;
  string roleSlug = 2;
  optional string name = 3;
  optional string description = 4;
  repeated string permissionSlugs = 5;
}

message DeleteRoleRequest {
  string actorUserId = 1;
  string roleSlug = 2;
}

message AssignUserRoleRequest {
  string actorUserId = 1;
  string userId = 2;
  string roleSlug = 3;
}

message AssignUserRoleResponse {
  string userId = 1;
  repeated string roles = 2;
}

message RemoveUserRoleRequest {
  string actorUserId = 1;
  string userId = 2;
  string roleSlug = 3;
}

message AssignUserPermissionRequest {
  string actorUserId = 1;
  string userId = 2;
  string permissionSlug = 3;
}

message AssignUserPermissionResponse {
  string userId = 1;
  repeated string permissions = 2;
}

message RemoveUserPermissionRequest {
  string actorUserId = 1;
  string userId = 2;
  string permissionSlug = 3;
}

message CreateOAuthClientRequest {
  string actorUserId = 1;
  string name = 2;
  repeated string redirectUris = 3;
  repeated string scopes = 4;
  optional string type = 5;
}

message UpdateOAuthClientRequest {
  string actorUserId = 1;
  string clientId = 2;
  optional string name = 3;
  repeated string redirectUris = 4;
  repeated string scopes = 5;
  optional bool isActive = 6;
}

message DeleteOAuthClientRequest {
  string actorUserId = 1;
  string clientId = 2;
}

message DeleteOAuthClientResponse {
  string clientId = 1;
}

message RotateOAuthSecretRequest {
  string actorUserId = 1;
  string clientId = 2;
}

message RotateOAuthSecretResponse {
  string clientId = 1;
  string clientSecret = 2;
}
