@@ -22,6 +22,23 @@ class CreateUserWithRLSPolicies extends Command
2222
2323 protected $ description = "Creates RLS policies for all tables related to the tenant table. Also creates the RLS user if it doesn't exist yet " ;
2424
25+ /**
26+ * Force, rather than just enable, the created RLS policies.
27+ *
28+ * By default, table owners bypass RLS policies. When this is enabled,
29+ * they also need the BYPASSRLS permission. If your setup lets you create
30+ * a user with BYPASSRLS, you may prefer leaving this on for additional
31+ * safety. Otherwise, if you can't use BYPASSRLS, you can set this to false
32+ * and depend on the behavior of table owners bypassing RLS automatically.
33+ *
34+ * This setting generally doesn't affect behavior at all with "default"
35+ * setups, however if you have a more custom setup, with additional users
36+ * involved (e.g. central connection user not being the same user that
37+ * creates tables, or the created "RLS user" creating some tables) you
38+ * should take care with how you configure this.
39+ */
40+ public static bool $ forceRls = true ;
41+
2542 public function handle (PermissionControlledPostgreSQLSchemaManager $ manager ): int
2643 {
2744 $ username = config ('tenancy.rls.user.username ' );
@@ -49,14 +66,9 @@ protected function enableRLS(string $table): void
4966 // Enable RLS scoping on the table (without this, queries won't be scoped using RLS)
5067 DB ::statement ("ALTER TABLE {$ table } ENABLE ROW LEVEL SECURITY " );
5168
52- /**
53- * Force RLS scoping on the table, so that the table owner users
54- * don't bypass the scoping – table owners bypass RLS by default.
55- *
56- * E.g. when using a custom implementation where you create tables as the RLS user,
57- * the queries won't be scoped for the RLS user unless we force the RLS scoping using this query.
58- */
59- DB ::statement ("ALTER TABLE {$ table } FORCE ROW LEVEL SECURITY " );
69+ if (static ::$ forceRls ) {
70+ DB ::statement ("ALTER TABLE {$ table } FORCE ROW LEVEL SECURITY " );
71+ }
6072 }
6173
6274 /**
0 commit comments