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

     1  //
     2  //  Copyright 2024 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  // IdentityMgr interface manages identities (users and groups).
    20  type IdentityMgr interface {
    21  	Featurer
    22  	OSTyper
    23  	Typer
    24  
    25  	// AdminGroup returns the administrator (root) group.
    26  	AdminGroup() GroupReader
    27  
    28  	// AdminUser returns the administrator (root) user.
    29  	AdminUser() UserReader
    30  
    31  	// GroupAdd adds a new group.
    32  	// If the group already exists, the returned error is of type AlreadyExistsGroupError.
    33  	GroupAdd(name string) (GroupReader, error)
    34  
    35  	// GroupDel deletes an existing group.
    36  	// If the group is not found, the returned error is of type UnknownGroupError.
    37  	GroupDel(name string) error
    38  
    39  	// LookupGroup looks up a group by name.
    40  	// If the group is not found, the returned error is of type UnknownGroupError.
    41  	LookupGroup(name string) (GroupReader, error)
    42  
    43  	// LookupGroupId looks up a group by groupid.
    44  	// If the group is not found, the returned error is of type UnknownGroupIdError.
    45  	LookupGroupId(gid int) (GroupReader, error)
    46  
    47  	// LookupUser looks up a user by username.
    48  	// If the user cannot be found, the returned error is of type UnknownUserError.
    49  	LookupUser(name string) (UserReader, error)
    50  
    51  	// LookupUserId looks up a user by userid.
    52  	// If the user cannot be found, the returned error is of type UnknownUserIdError.
    53  	LookupUserId(uid int) (UserReader, error)
    54  
    55  	// UserAdd adds a new user.
    56  	// If the user already exists, the returned error is of type AlreadyExistsUserError.
    57  	UserAdd(name, groupName string) (UserReader, error)
    58  
    59  	// UserDel deletes an existing user.
    60  	UserDel(name string) error
    61  }
    62  
    63  // UserReader reads user information.
    64  type UserReader interface {
    65  	GroupIdentifier
    66  	UserIdentifier
    67  	Namer
    68  
    69  	// IsAdmin returns true if the user has administrator (root) privileges.
    70  	IsAdmin() bool
    71  }
    72  
    73  // GroupIdentifier is the interface that wraps the Gid method.
    74  type GroupIdentifier interface {
    75  	// Gid returns the primary group id.
    76  	Gid() int
    77  }
    78  
    79  // GroupReader interface reads group information.
    80  type GroupReader interface {
    81  	GroupIdentifier
    82  	Namer
    83  }
    84  
    85  // UserIdentifier is the interface that wraps the Uid method.
    86  type UserIdentifier interface {
    87  	// Uid returns the user id.
    88  	Uid() int
    89  }
    90  
    91  // IdmMgr is the interface that wraps Identity manager setting methods for file systems.
    92  type IdmMgr interface {
    93  	// Idm returns the identity manager of the file system.
    94  	Idm() IdentityMgr
    95  
    96  	// SetIdm set the current identity manager.
    97  	// If the identity manager provider is nil, the idm dummyidm.NotImplementedIdm is set.
    98  	SetIdm(idm IdentityMgr) error
    99  }
   100  
   101  // IdmFn provides identity manager functions to a file system.
   102  type IdmFn struct {
   103  	idm IdentityMgr // idm is the identity manager of the file system.
   104  }
   105  
   106  // Idm returns the identity manager of the file system.
   107  func (idf *IdmFn) Idm() IdentityMgr {
   108  	return idf.idm
   109  }
   110  
   111  // SetIdm set the current identity manager.
   112  // If the identity manager provider is nil, the idm NotImplementedIdm is set.
   113  func (idf *IdmFn) SetIdm(idm IdentityMgr) error {
   114  	if idm == nil {
   115  		idm = NotImplementedIdm
   116  	}
   117  
   118  	idf.idm = idm
   119  
   120  	return nil
   121  }
   122  
   123  // AdminGroupName returns the name of the administrator group of the file system.
   124  func AdminGroupName(osType OSType) string {
   125  	switch osType {
   126  	case OsWindows:
   127  		return "Administrators"
   128  	default:
   129  		return "root"
   130  	}
   131  }
   132  
   133  // AdminUserName returns the name of the administrator of the file system.
   134  func AdminUserName(osType OSType) string {
   135  	switch osType {
   136  	case OsWindows:
   137  		return "ContainerAdministrator"
   138  	default:
   139  		return "root"
   140  	}
   141  }