github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/dummyidm.go (about)

     1  //
     2  //  Copyright 2021 The AVFS authors
     3  //
     4  //  Licensed under the Apache License, Version 2.0 (the "License");
     5  //  you may not use this file except in compliance with the License.
     6  //  You may obtain a copy of the License at
     7  //
     8  //  	http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  //  Unless required by applicable law or agreed to in writing, software
    11  //  distributed under the License is distributed on an "AS IS" BASIS,
    12  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  //  See the License for the specific language governing permissions and
    14  //  limitations under the License.
    15  //
    16  
    17  package avfs
    18  
    19  import (
    20  	"math"
    21  )
    22  
    23  // NotImplementedIdm is the default identity manager for all file systems.
    24  var NotImplementedIdm = NewDummyIdm() //nolint:gochecknoglobals // Used as default Idm for other file systems.
    25  
    26  // DummyIdm represent a non implemented identity manager using the avfs.IdentityMgr interface.
    27  type DummyIdm struct {
    28  	adminGroup GroupReader
    29  	adminUser  UserReader
    30  }
    31  
    32  // DummyUser is the implementation of avfs.UserReader.
    33  type DummyUser struct {
    34  	name string
    35  	uid  int
    36  	gid  int
    37  }
    38  
    39  // DummyGroup is the implementation of avfs.GroupReader.
    40  type DummyGroup struct {
    41  	name string
    42  	gid  int
    43  }
    44  
    45  // NewDummyIdm create a new identity manager.
    46  func NewDummyIdm() *DummyIdm {
    47  	return &DummyIdm{
    48  		adminGroup: &DummyGroup{name: DefaultName, gid: math.MaxInt},
    49  		adminUser:  &DummyUser{name: DefaultName, uid: math.MaxInt, gid: math.MaxInt},
    50  	}
    51  }
    52  
    53  func NewGroup(name string, gid int) *DummyGroup {
    54  	return &DummyGroup{name: name, gid: gid}
    55  }
    56  
    57  func NewUser(name string, uid, gid int) *DummyUser {
    58  	return &DummyUser{name: name, uid: uid, gid: gid}
    59  }
    60  
    61  // Type returns the type of the fileSystem or Identity manager.
    62  func (idm *DummyIdm) Type() string {
    63  	return "DummyIdm"
    64  }
    65  
    66  // Features returns the set of features provided by the file system or identity manager.
    67  func (idm *DummyIdm) Features() Features {
    68  	return 0
    69  }
    70  
    71  // HasFeature returns true if the file system or identity manager provides a given feature.
    72  func (idm *DummyIdm) HasFeature(feature Features) bool {
    73  	return false
    74  }
    75  
    76  // AdminGroup returns the administrators (root) group.
    77  func (idm *DummyIdm) AdminGroup() GroupReader {
    78  	return idm.adminGroup
    79  }
    80  
    81  // AdminUser returns the administrator (root) user.
    82  func (idm *DummyIdm) AdminUser() UserReader {
    83  	return idm.adminUser
    84  }
    85  
    86  // GroupAdd adds a new group.
    87  func (idm *DummyIdm) GroupAdd(name string) (GroupReader, error) {
    88  	return nil, ErrPermDenied
    89  }
    90  
    91  // GroupDel deletes an existing group.
    92  func (idm *DummyIdm) GroupDel(name string) error {
    93  	return ErrPermDenied
    94  }
    95  
    96  // LookupGroup looks up a group by name.
    97  // If the group cannot be found, the returned error is of type UnknownGroupError.
    98  func (idm *DummyIdm) LookupGroup(name string) (GroupReader, error) {
    99  	return nil, ErrPermDenied
   100  }
   101  
   102  // LookupGroupId looks up a group by groupid.
   103  // If the group cannot be found, the returned error is of type UnknownGroupIdError.
   104  func (idm *DummyIdm) LookupGroupId(gid int) (GroupReader, error) {
   105  	return nil, ErrPermDenied
   106  }
   107  
   108  // LookupUser looks up a user by username.
   109  // If the user cannot be found, the returned error is of type UnknownUserError.
   110  func (idm *DummyIdm) LookupUser(name string) (UserReader, error) {
   111  	return nil, ErrPermDenied
   112  }
   113  
   114  // LookupUserId looks up a user by userid.
   115  // If the user cannot be found, the returned error is of type UnknownUserIdError.
   116  func (idm *DummyIdm) LookupUserId(uid int) (UserReader, error) {
   117  	return nil, ErrPermDenied
   118  }
   119  
   120  // OSType returns the operating system type of the identity manager.
   121  func (idm *DummyIdm) OSType() OSType {
   122  	return CurrentOSType()
   123  }
   124  
   125  // UserAdd adds a new user.
   126  func (idm *DummyIdm) UserAdd(name, groupName string) (UserReader, error) {
   127  	return nil, ErrPermDenied
   128  }
   129  
   130  // UserDel deletes an existing group.
   131  func (idm *DummyIdm) UserDel(name string) error {
   132  	return ErrPermDenied
   133  }
   134  
   135  // DummyGroup
   136  
   137  // Gid returns the Group ID.
   138  func (g *DummyGroup) Gid() int {
   139  	return g.gid
   140  }
   141  
   142  // Name returns the Group name.
   143  func (g *DummyGroup) Name() string {
   144  	return g.name
   145  }
   146  
   147  // DummyUser
   148  
   149  // Gid returns the primary Group ID of the User.
   150  func (u *DummyUser) Gid() int {
   151  	return u.gid
   152  }
   153  
   154  // IsAdmin returns true if the user has administrator (root) privileges.
   155  func (u *DummyUser) IsAdmin() bool {
   156  	return u.uid == 0 || u.gid == 0
   157  }
   158  
   159  // Name returns the username.
   160  func (u *DummyUser) Name() string {
   161  	return u.name
   162  }
   163  
   164  // Uid returns the User ID.
   165  func (u *DummyUser) Uid() int {
   166  	return u.uid
   167  }