Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Añade compatibilidad con Directus 11. #29

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ type Client struct {
Collections *ResourceClient[Collection, string]
CustomTranslations *ResourceClient[CustomTranslation, string]
Folders *ResourceClient[Folder, string]
Roles *ResourceClient[Role, string]
Roles *clientRoles
rolesV10 *ResourceClient[roleV10, string]
rolesV11 *ResourceClient[roleV11, string]
Policies *ResourceClient[Policy, string]
Users *ResourceClient[User, string]
Presets *ResourceClient[Preset, int64]
Operations *ResourceClient[Operation, string]
Expand Down Expand Up @@ -65,7 +68,10 @@ func NewClient(instance string, token string, opts ...ClientOption) *Client {
client.Collections = NewResourceClient[Collection, string](client, "collections")
client.CustomTranslations = NewResourceClient[CustomTranslation, string](client, "translations")
client.Folders = NewResourceClient[Folder, string](client, "folders")
client.Roles = NewResourceClient[Role, string](client, "roles")
client.Roles = &clientRoles{client: client}
client.rolesV10 = NewResourceClient[roleV10, string](client, "roles")
client.rolesV11 = NewResourceClient[roleV11, string](client, "roles")
client.Policies = NewResourceClient[Policy, string](client, "policies")
client.Users = NewResourceClient[User, string](client, "users")
client.Presets = NewResourceClient[Preset, int64](client, "presets")
client.Operations = NewResourceClient[Operation, string](client, "operations")
Expand Down
24 changes: 24 additions & 0 deletions permissions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package directus

import (
"context"
"encoding/json"
"log/slog"
"testing"

"github.com/stretchr/testify/require"
)

func TestPermissionst(t *testing.T) {

permissions, err := initClient(t).Permissions.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, permissions)

for _, permission := range permissions {
p, err := json.Marshal(permission)
require.NoError(t, err)
slog.Info("Permission", slog.Any("permission", p))
}

}
33 changes: 33 additions & 0 deletions policies_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package directus

import (
"context"
"fmt"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestPoliciesList(t *testing.T) {

i, err := initClient(t).Server.Info(context.Background())
require.NoError(t, err)

major, err := strconv.Atoi(strings.Split(i.Version, ".")[0])
require.NoError(t, err)

if major < 11 {
t.Skip("Policies are only available in Directus 11 and above")
}

policies, err := initClient(t).Policies.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, policies)

for _, policy := range policies {
fmt.Printf("%#v\n", policy)
}

}
251 changes: 251 additions & 0 deletions roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package directus

import (
"context"
"fmt"
"sync"
)

type Role interface {
PoliciesKeys() ([]string, error)
ParentKey() (string, error)
ChildrenKeys() ([]string, error)
ServerVersion() *ServerInfo
}

type roleV10 struct {
ID string `json:"id,omitempty"`
Icon Icon `json:"icon,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`

AdminAccess *bool `json:"admin_access"`
AppAccess *bool `json:"app_access"`

Users []string `json:"users,omitempty"`

ServerInfo *ServerInfo
}

func (r *roleV10) ServerVersion() *ServerInfo {
return r.ServerInfo
}

func (r *roleV10) PoliciesKeys() ([]string, error) {
return nil, fmt.Errorf("directus: Policies are not supported in Directus below 11.0.0")
}

func (r *roleV10) ParentKey() (string, error) {
return "", fmt.Errorf("directus: Parent Role is not supported in Directus below 11.0.0")
}

func (r *roleV10) ChildrenKeys() ([]string, error) {
return nil, fmt.Errorf("directus: Children Roles are not supported in Directus below 11.0.0")
}

type roleV11 struct {
ID string `json:"id,omitempty"`
Icon Icon `json:"icon,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`

Policies []string `json:"policies,omitempty"`
Parent *string `json:"parent,omitempty"`
Children []string `json:"children,omitempty"`

Users []string `json:"users,omitempty"`

ServerInfo *ServerInfo
}

func (r *roleV11) ServerVersion() *ServerInfo {
return r.ServerInfo
}

func (r *roleV11) PoliciesKeys() ([]string, error) {
return r.Policies, nil
}

func (r *roleV11) ParentKey() (string, error) {
if r.Parent == nil {
return "", nil
}
return *r.Parent, nil
}

func (r *roleV11) ChildrenKeys() ([]string, error) {
return r.Children, nil
}

type clientRoles struct {
client *Client
mu sync.Mutex
}

func (cr *clientRoles) List(ctx context.Context) ([]Role, error) {
cr.mu.Lock()
defer cr.mu.Unlock()

inf, err := cr.client.Server.Info(ctx)
if err != nil {
return nil, err
}

switch {
// Support for Directus 10.0.X - 10.13.X
case inf.VersionMajor == 10 && (inf.VersionMinor >= 0 && inf.VersionMinor <= 13):
rolesV10, err := cr.client.rolesV10.List(ctx)
if err != nil {
return nil, fmt.Errorf("directus: cannot list roles: %w", err)
}
roles := make([]Role, len(rolesV10))
for i, r := range rolesV10 {
r.ServerInfo = inf
roles[i] = r
}

return roles, nil

// Support for Directus 11.0.X
case inf.VersionMajor == 11 && inf.VersionMinor == 0:
rolesV11, err := cr.client.rolesV11.List(ctx)
if err != nil {
return nil, fmt.Errorf("directus: cannot list roles: %w", err)
}
roles := make([]Role, len(rolesV11))
for i, r := range rolesV11 {
r.ServerInfo = inf
roles[i] = r
}
return roles, nil

default:
return nil, fmt.Errorf("directus: unsupported server version: %v", inf)
}
}

func (cr *clientRoles) Get(ctx context.Context, id string) (Role, error) {
cr.mu.Lock()
defer cr.mu.Unlock()

inf, err := cr.client.Server.Info(ctx)
if err != nil {
return nil, err
}

switch {
// Support for Directus 10.0.X - 10.13.X
case inf.VersionMajor == 10 && (inf.VersionMinor >= 0 && inf.VersionMinor <= 13):
roleV10, err := cr.client.rolesV10.Get(ctx, id)
if err != nil {
return nil, fmt.Errorf("directus: cannot get role: %w", err)
}
roleV10.ServerInfo = inf
return roleV10, nil

// Support for Directus 11.0.X
case inf.VersionMajor == 11 && inf.VersionMinor == 0:
roleV11, err := cr.client.rolesV11.Get(ctx, id)
if err != nil {
return nil, fmt.Errorf("directus: cannot get role: %w", err)
}
roleV11.ServerInfo = inf
return roleV11, nil

default:
return nil, fmt.Errorf("directus: unsupported server version: %v", inf)
}
}

func (cr *clientRoles) Create(ctx context.Context, role Role) (Role, error) {
cr.mu.Lock()
defer cr.mu.Unlock()

inf, err := cr.client.Server.Info(ctx)
if err != nil {
return nil, err
}

switch {
// Support for Directus 10.0.X - 10.13.X
case inf.VersionMajor == 10 && (inf.VersionMinor >= 0 && inf.VersionMinor <= 13):
roleV10, ok := role.(*roleV10)
if !ok {
return nil, fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV10.Create(ctx, roleV10)

// Support for Directus 11.0.X
case inf.VersionMajor == 11 && inf.VersionMinor == 0:
roleV11, ok := role.(*roleV11)
if !ok {
return nil, fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV11.Create(ctx, roleV11)

default:
return nil, fmt.Errorf("directus: unsupported server version: %v", inf)
}
}

func (cr *clientRoles) Delete(ctx context.Context, role Role) error {
cr.mu.Lock()
defer cr.mu.Unlock()

inf, err := cr.client.Server.Info(ctx)
if err != nil {
return err
}

switch {
// Support for Directus 10.0.X - 10.13.X
case inf.VersionMajor == 10 && (inf.VersionMinor >= 0 && inf.VersionMinor <= 13):
roleV10, ok := role.(*roleV10)
if !ok {
return fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV10.Delete(ctx, roleV10.ID)

// Support for Directus 11.0.X
case inf.VersionMajor == 11 && inf.VersionMinor == 0:
roleV11, ok := role.(*roleV11)
if !ok {
return fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV11.Delete(ctx, roleV11.ID)

default:
return fmt.Errorf("directus: unsupported server version: %v", inf)
}
}

func (cr *clientRoles) Patch(ctx context.Context, role Role) (Role, error) {
cr.mu.Lock()
defer cr.mu.Unlock()

inf, err := cr.client.Server.Info(ctx)
if err != nil {
return nil, err
}

switch {
// Support for Directus 10.0.X - 10.13.X
case inf.VersionMajor == 10 && (inf.VersionMinor >= 0 && inf.VersionMinor <= 13):
roleV10, ok := role.(*roleV10)
if !ok {
return nil, fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV10.Patch(ctx, roleV10.ID, roleV10)

// Support for Directus 11.0.X
case inf.VersionMajor == 11 && inf.VersionMinor == 0:
roleV11, ok := role.(*roleV11)
if !ok {
return nil, fmt.Errorf("directus: unsupported role type: %T", role)
}
return cr.client.rolesV11.Patch(ctx, roleV11.ID, roleV11)

default:
return nil, fmt.Errorf("directus: unsupported server version: %v", inf)
}
}
Loading