getPerm<T extends FrappeDocument> method

  1. @override
Future<RequestResponse<List<DocPerm>>> getPerm<T extends FrappeDocument>({
  1. required String? doctype,
  2. T? document,
})
override

Returns the list permission of the current user of a certain doctype.

Optionally can specify the permission of a certain document (TBD).

Implementation

@override
Future<RequestResponse<List<DocPerm>>> getPerm<T extends FrappeDocument>(
    {required String? doctype, T? document}) async {
  var perm = <DocPerm>[
    DocPerm()
      ..read = false
      ..ifOwner = false
      ..permLevel = 0
  ];

  if (getFrappeAuthController().currentUser == '<admin_user>') {
    perm[0].read = true;
  }

  final responses = await Future.wait([
    getFrappeMetaController().getDocMeta(doctype: doctype),
    getFrappeAuthController().getCurrentUserRoles()
  ]);
  final docMeta = responses[0] as RequestResponse<DocType?>;
  final currentUserRoles = responses[1] as RequestResponse<List<String>?>?;

  if (!docMeta.isSuccess) {
    return RequestResponse.success(perm);
  }

  if (!currentUserRoles!.isSuccess) {
    return RequestResponse.success(perm);
  }

  // read from meta.permissions
  for (final doctypePerm in docMeta.data!.permissions!) {
    // apply only if this DocPerm role is present for currentUser
    if (!currentUserRoles.data!.contains(doctypePerm.role)) {
      continue;
    }

    // permlevels 0,1,2..
    // There's a new perm level, need to grow the list
    if (perm.length < doctypePerm.permLevel! + 1) {
      perm.add(DocPerm());
      perm[doctypePerm.permLevel!].permLevel = doctypePerm.permLevel;
    }

    // For User permissions
    // NOTE: this data is required for displaying match rules in ListComponent
    for (final pType in PermissionType.values) {
      final permission = perm[doctypePerm.permLevel!].toJson();
      final permissionType = EnumToString.convertToString(pType);
      permission[permissionType] = permission[permissionType] == 1
          ? permission[permissionType]
          : (doctypePerm.toJson()[permissionType] ?? 0);
      perm[doctypePerm.permLevel!] = DocPerm.fromJson(permission);
    }
  }
  // TODO : Implement Document Specific rules
  return RequestResponse.success(perm);
}