Skip to content

Commit

Permalink
feat: added additional domains for ubuntu one demo
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Apr 24, 2024
1 parent c8eed66 commit 43c9b15
Show file tree
Hide file tree
Showing 59 changed files with 2,199 additions and 787 deletions.
10 changes: 9 additions & 1 deletion app/controllers/web/my-account/create-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ async function createAlias(ctx, next) {
// if the domain is ubuntu.com and the user is in the user group
// then don't allow them to create aliases (only manage/delete their own)
//
if (ctx.state.domain.name === 'ubuntu.com') {
if (
[
'ubuntu.com',
'kubuntu.org',
'lubuntu.me',
'edubuntu.org',
'ubuntustudio.com'
].includes(ctx.state.domain.name)
) {
const member = ctx.state.domain.members.find(
(member) => member.user && member.user.id === ctx.state.user.id
);
Expand Down
14 changes: 12 additions & 2 deletions app/controllers/web/my-account/validate-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ function validateAlias(ctx, next) {
// if the domain is ubuntu.com and the user is in the user group
// then don't allow them to enable IMAP
//
if (ctx.state.domain.name === 'ubuntu.com') {
if (
[
'ubuntu.com',
'kubuntu.org',
'lubuntu.me',
'edubuntu.org',
'ubuntustudio.com'
].includes(ctx.state.domain.name)
) {
const member = ctx.state.domain.members.find(
(member) => member.user && member.user.id === ctx.state.user.id
);
Expand All @@ -120,7 +128,9 @@ function validateAlias(ctx, next) {

if (
_.isArray(body.recipients) &&
body.recipients.some((r) => isEmail(r) && r.endsWith('@ubuntu.com'))
body.recipients.some(
(r) => isEmail(r) && r.endsWith(`@${ctx.state.domain.name}`)
)
)
return ctx.throw(
Boom.notFound(ctx.translateError('UBUNTU_NOT_ALLOWED_EMAIL'))
Expand Down
195 changes: 109 additions & 86 deletions app/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,14 +941,115 @@ Users.pre('save', async function (next) {

// TODO: support pagination for users that have paginated memberships

if (
_.isEmpty(json.entries) ||
json.total_size === 0 ||
!json.entries.some(
(entry) =>
entry.team_link === 'https://api.launchpad.net/1.0/~ubuntumembers'
)
) {
// TODO: move this to config index and rewrite everywhere else to re-use
const mapping = {
'ubuntu.com': '~ubuntumembers',
'kubuntu.org': '~kubuntu-members',
'lubuntu.me': '~lubuntu-members',
'edubuntu.org': '~edubuntu-members',
'ubuntustudio.com': '~ubuntustudio'
};

let hasMatch = !_.isEmpty(json.entries) && json.total_size !== 0;

//
// now we need to find the @ubuntu.com domain
// and create the user their alias if not already exists
//
const adminIds = await this.constructor.distinct('_id', {
group: 'admin'
});

if (!hasMatch) {
for (const domainName of Object.keys(mapping)) {
if (
json.entries.some(
(entry) =>
entry.team_link ===
`https://api.launchpad.net/1.0/${mapping[domainName]}`
)
) {
hasMatch = true;

// eslint-disable-next-line no-await-in-loop
const domain = await conn.models.Domains.findOne({
name: domainName,
plan: 'team',
'members.user': {
$in: adminIds
}
});

if (!domain) {
const error = Boom.badRequest(
i18n.api.t({
phrase: config.i18n.phrases.DOMAIN_DOES_NOT_EXIST_ANYWHERE,
locale: this[config.lastLocaleField]
})
);
error.no_translate = true;
throw error;
}

//
// otherwise check that the domain includes this user id
// and if not, then add it to the group as a user
//
const match = domain.members.find(
(m) => m.user.toString() === this._id.toString()
);
if (!match) {
domain.members.push({
user: this._id,
group: 'user'
});
// eslint-disable-next-line no-await-in-loop
await domain.save();
}

// now check that if the alias already exists and is owned by this user
// eslint-disable-next-line no-await-in-loop
const alias = await conn.models.Aliases.findOne({
user: this._id,
domain: domain._id,
name: this[fields.ubuntuUsername].toLowerCase()
});

// if not, then create it, but only if there aren't already 3+ aliases owned by this user
if (!alias) {
// eslint-disable-next-line no-await-in-loop
const count = await conn.models.Aliases.countDocuments({
user: this._id,
domain: domain._id
});
if (count > 3) {
const error = Boom.badRequest(
i18n.api.t({
phrase: config.i18n.phrases.UBUNTU_MAX_LIMIT,
locale: this[config.lastLocaleField]
})
);
error.no_translate = true;
throw error;
}

// eslint-disable-next-line no-await-in-loop
await conn.models.Aliases.create({
// virtual to assist in preventing lookup
is_new_user: true,

user: this._id,
domain: domain._id,
name: this[fields.ubuntuUsername].toLowerCase(),
recipients: [this.email],
locale: this[config.lastLocaleField]
});
}
}
}
}

if (!hasMatch) {
const error = Boom.badRequest(
i18n.api.t({
phrase: config.i18n.phrases.UBUNTU_INVALID_GROUP,
Expand Down Expand Up @@ -983,84 +1084,6 @@ Users.pre('save', async function (next) {
}
*/

//
// now we need to find the @ubuntu.com domain
// and create the user their alias if not already exists
//
const adminIds = await this.constructor.distinct('_id', {
group: 'admin'
});

const domain = await conn.models.Domains.findOne({
name: 'ubuntu.com',
plan: 'team',
'members.user': {
$in: adminIds
}
});

if (!domain) {
const error = Boom.badRequest(
i18n.api.t({
phrase: config.i18n.phrases.DOMAIN_DOES_NOT_EXIST_ANYWHERE,
locale: this[config.lastLocaleField]
})
);
error.no_translate = true;
throw error;
}

//
// otherwise check that the domain includes this user id
// and if not, then add it to the group as a user
//
const match = domain.members.find(
(m) => m.user.toString() === this._id.toString()
);
if (!match) {
domain.members.push({
user: this._id,
group: 'user'
});
await domain.save();
}

// now check that if the alias already exists and is owned by this user
const alias = await conn.models.Aliases.findOne({
user: this._id,
domain: domain._id,
name: this[fields.ubuntuUsername].toLowerCase()
});

// if not, then create it, but only if there aren't already 3+ aliases owned by this user
if (!alias) {
const count = await conn.models.Aliases.countDocuments({
user: this._id,
domain: domain._id
});
if (count > 3) {
const error = Boom.badRequest(
i18n.api.t({
phrase: config.i18n.phrases.UBUNTU_MAX_LIMIT,
locale: this[config.lastLocaleField]
})
);
error.no_translate = true;
throw error;
}

await conn.models.Aliases.create({
// virtual to assist in preventing lookup
is_new_user: true,

user: this._id,
domain: domain._id,
name: this[fields.ubuntuUsername].toLowerCase(),
recipients: [this.email],
locale: this[config.lastLocaleField]
});
}

next();
} catch (err) {
next(err);
Expand Down
2 changes: 1 addition & 1 deletion app/views/_breadcrumbs.pug
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ if breadcrumbs.length > 1
!= t('Import <span class="notranslate">TXT</span> Records')
= " "
i.fa.fa-cloud-download-alt
if domain.name !== 'ubuntu.com' || domain.group === 'admin'
if domain.group === 'admin' || ['ubuntu.com','kubuntu.org','lubuntu.me','edubuntu.org','ubuntustudio.com'].includes(domain.name)
li.list-inline-item.mb-1
a.btn.btn-dark(
href=l(`/my-account/domains/${domain.name}/aliases/new`),
Expand Down
Loading

0 comments on commit 43c9b15

Please sign in to comment.