guard method
- String ptr,
- List<
LicenseUsecase> usecases, { - String? origin,
- List<
String> ? destinations, - dynamic onPass()?,
- dynamic onFail(
- String
Guard against an invalid LicenseRecord for a List of usecases
and
destinations
.
Use this method to verify a non-expired LicenseRecord for the ptr
exists and permits the listed usecases
and destinations
.
Parameters:
• ptr
- The Pointer Record for the asset. Used to located the latest
relevant LicenseRecord.
• origin
- An optional override of the default origin
specified in
init.
• usecases
- A List of usecases defining how the asset will be used.
• destinations
- A List of destinations defining where the asset will
be used. Often URLs
• onPass
- A Function to execute automatically upon successfully
resolving the LicenseRecord against the usecases
and destinations
• onFail
- A Function to execute automatically upon failure to resolve
the LicenseRecord. Accepts a String parameter, holding an error
message describing the reason for failure.
This method can be used in two forms, 1) as a traditional guard, returning a pass/fail boolean. Or 2) as a wrapper around function.
For example: An http that you want to run IF permitted by a LicenseRecord.
Option 1:
bool pass = guard('ptr', [LicenseUsecase.attribution()]);
if(pass) http.post(...);
Option 2:
guard('ptr', [LicenseUsecase.attribution()], onPass: () => http.post(...));
Returns the created TitleRecord
Implementation
bool guard(String ptr, List<LicenseUsecase> usecases,
{String? origin,
List<String>? destinations,
Function()? onPass,
Function(String)? onFail}) {
TitleRecord? title = this.title.get(ptr, origin: origin);
if (title == null) {
if (onFail != null) onFail('Missing title for ptr: $ptr');
return false;
}
LicenseRecord? license = this.license.latest(title);
if (license == null) {
if (onFail != null) onFail('Missing license for ptr: $ptr');
return false;
}
String? guardMessage = Guard.check(
license, [LicenseUse(usecases, destinations: destinations)]);
if (guardMessage == null) {
if (onPass != null) onPass();
return true;
} else {
if (onFail != null) onFail(guardMessage);
return false;
}
}