Skip to content

Core\Cap::has

Says whether the program is allowed to do something, so it can take another route when it is not.

A deployment decides what each program may do: read files, connect to another server, run another script, reach a database. Each of these permissions is called a capability and has a name, such as fs.read. Core\Cap::has takes one of those names and returns true or false. A feature that is useful but not required checks first, and does something else when the answer is false.

Good to know: the check changes nothing. A program can never give itself a capability it was not given. A true is also not a promise about one file or one server. It means the capability was granted for something. The exact file is checked again when the program opens it, and that check can still say no.

The examples below check what a program may do, show what a true does not cover, and switch an optional feature off.

Parameters

  • $capabilitystringA capability name as nvs.toml writes it, such as fs.read or net.connect. A literal name that is not a capability does not compile. A computed name that is not a capability returns false.

Return value

Returns bool. true when the capability is granted for at least something, and false otherwise. A true does not mean every path or host is allowed. A grant limited to some paths still throws an error for a path outside them.

Examples

The examples below run with this configuration file.

nvs.toml
# The program may not write any file.
# A program run with `nvs run` may write every file when this line is missing.
[capabilities.fs]
write = []
# The second example may read files only in the `data` directory beside this file.
[[app]]
entry = "02-what-a-yes-does-not-cover.nvs"
[app.capabilities.fs]
read = ["data"]

What this program may do

<?nvs
// Shows how a program checks what it is allowed to do. This deployment granted
// it reading files, and nothing else.
// `Core\Cap::has` returns `true` when the capability is granted. This prints yes.
echo 'read files: ', Core\Cap::has('fs.read') ? 'yes' : 'no', "\n";
// A capability nobody granted returns `false`. This prints no.
echo 'write files: ', Core\Cap::has('fs.write') ? 'yes' : 'no', "\n";
// The names are the ones the deployment writes in `nvs.toml`. This prints no.
echo 'send mail: ', Core\Cap::has('mail.send') ? 'yes' : 'no', "\n";
Output
read files: yes
write files: no
send mail: no

What a yes does not cover

<?nvs
// Shows what a `true` covers. This program may read files in one directory.
// The capability is granted, and the exact file is checked again when the
// program opens it.
// The deployment granted `fs.read`, so this prints yes.
echo 'read files: ', Core\Cap::has('fs.read') ? 'yes' : 'no', "\n";
// A file inside the granted directory opens.
echo Core\Str::trim(Core\IO::read('data/hours.txt')), "\n";
// A file outside that directory throws an error, even though the result above was yes.
// The message continues with the path, so the program prints only its first part.
try {
Core\IO::read('nvs.toml');
} catch (RuntimeError $denied) {
echo Core\Str::before($denied->message, ' for '), "\n";
}
Output
read files: yes
Opening hours: 09:00 to 18:00.
Core\IO::read needs the capability `fs.read`

An optional feature

<?nvs
// Shows the everyday use: a feature the program can work without. It writes an
// audit file where it is allowed to, and prints the lines where it is not. This
// deployment did not grant writing files.
class Audit
{
public static function record(string $line): void
{
// The program takes the other route when the answer is `false`, instead
// of stopping with an error.
if (Core\Cap::has('fs.write')) {
Core\IO::append('var/audit.log', $line . "\n");
return;
}
echo 'audit: ', $line, "\n";
}
}
Audit::record('order 1042 paid');
Audit::record('order 1042 shipped');
Output
audit: order 1042 paid
audit: order 1042 shipped