pub enum UserId {
SystemWide(String),
Namespace(NamespaceId, String),
}
Expand description
The ID for a NetHsm
user
UserId
s are an essential part of the user management for a NetHSM.
They come in two types: system-wide and in a namespace.
UserId
s for system-wide users only consist of characters in the set [a-z0-9]
(e.g.
user1
) and must be at least one char long.
The UserId
s of users in a namespace consist of characters in the set [a-z0-9~]
and
contain the name of the namespace (see NamespaceId
) they are in. These UserId
s must be
at least three chars long. The ~
character serves as delimiter between the namespace part and
the user part (e.g. namespace1~user1
).
Variants§
Implementations§
Source§impl UserId
impl UserId
Sourcepub fn new(user_id: String) -> Result<Self, Error>
pub fn new(user_id: String) -> Result<Self, Error>
Creates a new UserId
from owned String
The provided string must be in the character set [a-z0-9~]
and at least one char long. The
~
character can not be used as the first character and can only occur once.
§Errors
Returns an Error
if
- the provided string contains an invalid character
- the
~
character is used as the first character - the
~
character is used more than once
§Examples
use nethsm::UserId;
// the UserId of a system-wide user
assert!(UserId::new("user1".to_string()).is_ok());
// the UserId of a namespace user
assert!(UserId::new("namespace1~user1".to_string()).is_ok());
// the input can not contain invalid chars
assert!(UserId::new("user1X".to_string()).is_err());
assert!(UserId::new("user;-".to_string()).is_err());
// the '~' character must be surrounded by other characters and only occur once
assert!(UserId::new("~user1".to_string()).is_err());
assert!(UserId::new("namespace~user~else".to_string()).is_err());
Sourcepub fn namespace(&self) -> Option<String>
pub fn namespace(&self) -> Option<String>
Returns the namespace of the UserId
§Examples
use nethsm::UserId;
// the UserId of a system-wide user
assert_eq!(UserId::new("user1".to_string())?.namespace(), None);
// the UserId of a namespace user
assert_eq!(
UserId::new("namespace1~user1".to_string())?.namespace(),
Some("namespace1".to_string())
);
Sourcepub fn is_namespaced(&self) -> bool
pub fn is_namespaced(&self) -> bool
Sourcepub fn validate_namespace_access(
&self,
support: NamespaceSupport,
target: Option<&UserId>,
role: Option<&UserRole>,
) -> Result<(), Error>
pub fn validate_namespace_access( &self, support: NamespaceSupport, target: Option<&UserId>, role: Option<&UserRole>, ) -> Result<(), Error>
Validates whether the UserId
can be used in a given context
Ensures that UserId
can be used in its context (e.g. calls to system-wide or
namespace resources) by defining namespace support
of the context.
Additionally ensures the validity of calls to resources targeting other users (provided by
target
), which are themselves system-wide or in a namespace.
When role
is provided, the validity of targeting the UserRole
is evaluated.
§Errors
This call returns an
Error::NamespaceTargetMismatch
if a user in one namespace tries to target a user in another namespaceError::NamespaceRoleInvalid
, if a user in a namespace targets a user in theBackup
orMetrics
role, or if a user not in a namespace targets a namespaced user in theBackup
orMetrics
role.Error::NamespaceSystemWideTarget
, if a user in a namespace targets a system-wide user