Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2026 LiteFarm.org
* This file is part of LiteFarm.
*
* LiteFarm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiteFarm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/

// As of this migration, new SSO users receive a UUID user_id. The Google sub is stored in google_sub.

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const up = async function (knex) {
await knex.schema.alterTable('users', function (table) {
table.boolean('deleted').notNullable().defaultTo(false);
table.string('google_sub').nullable();
});

await knex.raw(`UPDATE users SET google_sub = user_id WHERE user_id ~ '^\\d+$'`);

await knex.schema.alterTable('users', function (table) {
table.index('google_sub');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const down = async function (knex) {
await knex.schema.alterTable('users', function (table) {
table.dropIndex('google_sub');
table.dropColumn('deleted');
table.dropColumn('google_sub');
});
};
14 changes: 8 additions & 6 deletions packages/api/src/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class User extends Model {
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
this.updated_at = new Date().toISOString();
!queryContext.shouldUpdateEmail && delete this.email;
if (!queryContext.shouldUpdateEmail) delete this.email;
}

async $beforeInsert(context) {
await super.$beforeInsert(context);
this.email && (this.email = this.email.toLowerCase());
this.first_name && (this.first_name = this.first_name.trim());
this.last_name && (this.last_name = this.last_name.trim());
if (this.email) this.email = this.email.toLowerCase();
if (this.first_name) this.first_name = this.first_name.trim();
if (this.last_name) this.last_name = this.last_name.trim();
}

static async beforeFind(args) {
await super.beforeFind(args);
this.email && (this.email = this.email.toLowerCase());
if (this.email) this.email = this.email.toLowerCase();
}

static get tableName() {
Expand All @@ -55,7 +55,7 @@ class User extends Model {
}

static get hidden() {
return ['created_at', 'updated_at'];
return ['created_at', 'updated_at', 'deleted', 'google_sub'];
}

static get hiddenFromOtherUsers() {
Expand Down Expand Up @@ -130,6 +130,8 @@ class User extends Model {
do_not_email: { type: 'boolean' },
created_at: { type: 'string', format: 'date-time' },
updated_at: { type: 'string', format: 'date-time' },
deleted: { type: 'boolean' },
google_sub: { type: ['string', 'null'] },
},
additionalProperties: false,
};
Expand Down
Loading