Page MenuHomePhabricator

No OneTemporary

diff --git a/server/src/scripts/create-sidebar-permissions.js b/server/src/scripts/create-sidebar-permissions.js
new file mode 100644
index 000000000..2039c324a
--- /dev/null
+++ b/server/src/scripts/create-sidebar-permissions.js
@@ -0,0 +1,69 @@
+// @flow
+
+import {
+ threadPermissions,
+ threadPermissionPrefixes,
+ assertThreadType,
+} from 'lib/types/thread-types';
+
+import bots from 'lib/facts/bots';
+
+import { dbQuery, SQL } from '../database/database';
+import { endScript } from './utils';
+import { createScriptViewer } from '../session/scripts';
+import {
+ recalculateAllPermissions,
+ commitMembershipChangeset,
+} from '../updaters/thread-permission-updaters';
+
+async function main() {
+ try {
+ await createSidebarPermissions();
+ await updateAllThreadPermissions();
+ } catch (e) {
+ console.warn(e);
+ } finally {
+ endScript();
+ }
+}
+
+async function createSidebarPermissions() {
+ const createSidebarsString = `$.${threadPermissions.CREATE_SIDEBARS}`;
+ const updateAllRoles = SQL`
+ UPDATE roles
+ SET permissions = JSON_SET(permissions, ${createSidebarsString}, TRUE)
+ `;
+ await dbQuery(updateAllRoles);
+
+ const descendantSidebarsString =
+ `$.${threadPermissionPrefixes.DESCENDANT}` +
+ threadPermissions.CREATE_SIDEBARS;
+ const updateAdminRoles = SQL`
+ UPDATE roles
+ SET permissions = JSON_SET(permissions, ${descendantSidebarsString}, TRUE)
+ WHERE name = 'Admins'
+ `;
+ await dbQuery(updateAdminRoles);
+}
+
+async function updateAllThreadPermissions() {
+ const getAllThreads = SQL`SELECT id, type FROM threads`;
+ const [result] = await dbQuery(getAllThreads);
+
+ // We handle each thread one-by-one to avoid a situation where a permission
+ // calculation for a child thread, done during a call to
+ // recalculateAllPermissions for the parent thread, can be incorrectly
+ // overriden by a call to recalculateAllPermissions for the child thread. If
+ // the changeset resulting from the parent call isn't committed before the
+ // calculation is done for the child, the calculation done for the child can
+ // be incorrect.
+ const viewer = createScriptViewer(bots.squadbot.userID);
+ for (const row of result) {
+ const threadID = row.id.toString();
+ const threadType = assertThreadType(row.type);
+ const changeset = await recalculateAllPermissions(threadID, threadType);
+ await commitMembershipChangeset(viewer, changeset);
+ }
+}
+
+main();

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 10:38 AM (18 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2690780
Default Alt Text
(2 KB)

Event Timeline