This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/models/AuthenticationProvider.js

65 lines
1.4 KiB
JavaScript

// @flow
import providers from "../auth/providers";
import { ValidationError } from "../errors";
import { DataTypes, Op, sequelize } from "../sequelize";
const AuthenticationProvider = sequelize.define(
"authentication_providers",
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
validate: {
isIn: [providers.map((p) => p.id)],
},
},
enabled: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
providerId: {
type: DataTypes.STRING,
},
},
{
timestamps: true,
updatedAt: false,
}
);
AuthenticationProvider.associate = (models) => {
AuthenticationProvider.belongsTo(models.Team);
AuthenticationProvider.hasMany(models.UserAuthentication);
};
AuthenticationProvider.prototype.disable = async function () {
const res = await AuthenticationProvider.findAndCountAll({
where: {
teamId: this.teamId,
enabled: true,
id: {
[Op.ne]: this.id,
},
},
limit: 1,
});
if (res.count >= 1) {
return this.update({ enabled: false });
} else {
throw new ValidationError(
"At least one authentication provider is required"
);
}
};
AuthenticationProvider.prototype.enable = async function () {
return this.update({ enabled: true });
};
export default AuthenticationProvider;