From 902bb8e2cfe652066858ad6bd299128823590859 Mon Sep 17 00:00:00 2001 From: Giuseppe Lo Presti Date: Thu, 24 Jun 2021 13:08:10 +0200 Subject: [PATCH] Redefined PermissionNone to be more future-proof --- .../owncloud/ocs/conversions/permissions.go | 16 ++++++++++------ .../owncloud/ocs/conversions/permissions_test.go | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/http/services/owncloud/ocs/conversions/permissions.go b/internal/http/services/owncloud/ocs/conversions/permissions.go index e21f8628703..4c11fea8449 100644 --- a/internal/http/services/owncloud/ocs/conversions/permissions.go +++ b/internal/http/services/owncloud/ocs/conversions/permissions.go @@ -38,23 +38,27 @@ const ( PermissionDelete // PermissionShare grants share permissions on a resource PermissionShare - // PermissionAll grants all permissions on a resource - PermissionAll Permissions = (1 << (iota - 1)) - 1 // PermissionNone grants no permissions on a resource - PermissionNone = PermissionAll + 1 + PermissionNone + // PermissionMax is to be used within value range checks + PermissionMax Permissions = (1 << (iota - 1)) - 1 + // PermissionAll grants all permissions on a resource + PermissionAll = PermissionMax - PermissionNone + // PermissionMin is to be used within value range checks + PermissionMin = PermissionRead ) var ( // ErrPermissionNotInRange defines a permission specific error. - ErrPermissionNotInRange = fmt.Errorf("The provided permission is not between %d and %d", PermissionRead, PermissionNone) + ErrPermissionNotInRange = fmt.Errorf("The provided permission is not between %d and %d", PermissionMin, PermissionMax) ) // NewPermissions creates a new Permissions instance. // The value must be in the valid range. func NewPermissions(val int) (Permissions, error) { if val == int(PermissionInvalid) { - return PermissionInvalid, fmt.Errorf("permissions %d out of range %d - %d", val, PermissionRead, PermissionNone) - } else if val < int(PermissionInvalid) || int(PermissionNone) < val { + return PermissionInvalid, fmt.Errorf("permissions %d out of range %d - %d", val, PermissionMin, PermissionMax) + } else if val < int(PermissionInvalid) || int(PermissionMax) < val { return PermissionInvalid, ErrPermissionNotInRange } return Permissions(val), nil diff --git a/internal/http/services/owncloud/ocs/conversions/permissions_test.go b/internal/http/services/owncloud/ocs/conversions/permissions_test.go index 6ad7c39de8f..59d39903fc9 100644 --- a/internal/http/services/owncloud/ocs/conversions/permissions_test.go +++ b/internal/http/services/owncloud/ocs/conversions/permissions_test.go @@ -23,7 +23,7 @@ import ( ) func TestNewPermissions(t *testing.T) { - for val := int(PermissionRead); val <= int(PermissionNone); val++ { + for val := int(PermissionMin); val <= int(PermissionMax); val++ { _, err := NewPermissions(val) if err != nil { t.Errorf("value %d should be a valid permissions", val) @@ -35,7 +35,7 @@ func TestNewPermissionsWithInvalidValueShouldFail(t *testing.T) { vals := []int{ int(PermissionInvalid), -1, - int(PermissionNone) + 1, + int(PermissionMax) + 1, } for _, v := range vals { _, err := NewPermissions(v)