attachRole method

Future<bool> attachRole(
  1. RoleType role, {
  2. DatabaseDisk disk = Model.defaultDisk,
})

Attaches the given role to the user.

If the role record exists and the user does not already have it, the role is attached through the user/role pivot table.

Returns true when the role is attached successfully.

Example:

await user.attachRole(RoleType.staff);

Implementation

Future<bool> attachRole(RoleType role, {DatabaseDisk disk = Model.defaultDisk}) async {
  final roleRecord = await Model.firstWhere<Role>(field: 'name', value: role.name);
  if (roleRecord != null) {
    if (!await hasRole(role)) {
      return attach(roleRecord, relationship: .belongsToMany, table: UserRolePivotTable(), disk: disk);
    }
    return false;
  }
  return false;
}