updateUserScopes static method

Future<UserInfo?> updateUserScopes(
  1. Session session,
  2. int userId,
  3. Set<Scope> newScopes
)

Updates the scopes a user can access.

Implementation

static Future<UserInfo?> updateUserScopes(
  Session session,
  int userId,
  Set<Scope> newScopes,
) async {
  var userInfo = await findUserByUserId(session, userId, useCache: false);
  if (userInfo == null) return null;

  var scopeStrs = <String>[];
  for (var scope in newScopes) {
    if (scope.name != null) scopeStrs.add(scope.name!);
  }
  userInfo.scopeNames = scopeStrs;
  await session.db.update(userInfo);

  // Update all authentication keys too.
  var json = SerializationManager.encode(scopeStrs);
  await session.db.query(
      'UPDATE serverpod_auth_key SET "scopeNames"=\'$json\' WHERE "userId" = $userId');

  if (AuthConfig.current.onUserUpdated != null) {
    await AuthConfig.current.onUserUpdated!(session, userInfo);
  }

  await invalidateCacheForUser(session, userId);
  return userInfo;
}