Skip to content

Commit

Permalink
Add permissions APIs and values (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Oct 26, 2023
1 parent c924f19 commit 81a80f8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/src/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import '../gen/app/v1/app.pbgrpc.dart';
import 'permissions.dart';

typedef RobotPartLogPage = GetRobotPartLogsResponse;

Expand Down Expand Up @@ -86,4 +87,26 @@ class AppClient {
final stream = response.map((event) => event.logs);
return stream.asBroadcastStream(onCancel: (_) => response.cancel());
}

/// List the [Authorization]s available for the currently authenticated user
Future<List<Authorization>> listAuthorizations(String organizationId, {List<String> resourceIds = const []}) async {
final request = ListAuthorizationsRequest()
..organizationId = organizationId
..resourceIds.addAll(resourceIds);
final response = await _client.listAuthorizations(request);
return response.authorizations;
}

Future<List<Permission>> checkPermissions(ResourceType resourceType, String resourceId, List<Permission> permissions) async {
final request = CheckPermissionsRequest()
..permissions.add((AuthorizedPermissions()
..resourceType = resourceType.name
..resourceId = resourceId
..permissions.addAll(permissions.map((e) => e.value))));
final response = await _client.checkPermissions(request);
if (response.authorizedPermissions.isEmpty) return [];
return response.authorizedPermissions.first.permissions
.map((e) => Permission.values.firstWhere((element) => element.value == e))
.toList();
}
}
39 changes: 39 additions & 0 deletions lib/src/app/permissions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
enum Role {
organizationOwner,
organizationOperator,
robotOwner,
robotOperator,
locationOwner,
locationOperator;

String get value {
final exp = RegExp('(?<=[a-z])[A-Z]');
return name.replaceAllMapped(exp, (m) => '_${m.group(0)}').toLowerCase();
}
}

enum ResourceType { location, organization, robot }

enum Permission {
readOrganization,
writeOrganization,
readFragment,
writeFragment,
readLocation,
writeLocation,
readRobot,
readRobotConfig,
writeRobot,
controlRobot,
readOrganizationDataManagement,
readLocationDataManagement,
readRobotDataManagement,
writeDataManagement,
readLocationSecret,
readRobotSecret;

String get value {
final exp = RegExp('(?<=[a-z])[A-Z]');
return name.replaceAllMapped(exp, (m) => '_${m.group(0)}').toLowerCase();
}
}
1 change: 1 addition & 0 deletions lib/viam_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library viam_sdk;

/// App Clients
export 'src/app/data.dart' hide DataClient;
export 'src/app/permissions.dart';

/// Components
export 'src/components/arm/arm.dart';
Expand Down

0 comments on commit 81a80f8

Please sign in to comment.