github.com/cs3org/reva/v2@v2.27.7/pkg/conversions/permissions.go (about)

     1  // Copyright 2020 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package conversions
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  	"strconv"
    25  )
    26  
    27  // Permissions reflects the CRUD permissions used in the OCS sharing API
    28  type Permissions uint
    29  
    30  const (
    31  	// PermissionInvalid represents an invalid permission
    32  	PermissionInvalid Permissions = 0
    33  	// PermissionRead grants read permissions on a resource
    34  	PermissionRead Permissions = 1 << (iota - 1)
    35  	// PermissionWrite grants write permissions on a resource
    36  	PermissionWrite
    37  	// PermissionCreate grants create permissions on a resource
    38  	PermissionCreate
    39  	// PermissionDelete grants delete permissions on a resource
    40  	PermissionDelete
    41  	// PermissionShare grants share permissions on a resource
    42  	PermissionShare
    43  	// PermissionAll grants all permissions on a resource
    44  	PermissionAll Permissions = (1 << (iota - 1)) - 1
    45  	// PermissionMaxInput is to be used within value range checks
    46  	PermissionMaxInput = PermissionAll
    47  	// PermissionMinInput is to be used within value range checks
    48  	PermissionMinInput = PermissionRead
    49  	// PermissionsNone is to be used to deny access on a resource
    50  	PermissionsNone = 64
    51  )
    52  
    53  var (
    54  	// ErrPermissionNotInRange defines a permission specific error.
    55  	ErrPermissionNotInRange = fmt.Errorf("The provided permission is not between %d and %d", PermissionMinInput, PermissionMaxInput)
    56  	// ErrZeroPermission defines a permission specific error
    57  	ErrZeroPermission = errors.New("permission is zero")
    58  )
    59  
    60  // NewPermissions creates a new Permissions instance.
    61  // The value must be in the valid range.
    62  func NewPermissions(val int) (Permissions, error) {
    63  	if val == int(PermissionInvalid) {
    64  		return PermissionInvalid, ErrZeroPermission
    65  	} else if val < int(PermissionInvalid) || int(PermissionMaxInput) < val {
    66  		return PermissionInvalid, ErrPermissionNotInRange
    67  	}
    68  	return Permissions(val), nil
    69  }
    70  
    71  // Contain tests if the permissions contain another one.
    72  func (p Permissions) Contain(other Permissions) bool {
    73  	return p&other == other
    74  }
    75  
    76  func (p Permissions) String() string {
    77  	return strconv.FormatUint(uint64(p), 10)
    78  }