Skip to content

Commit

Permalink
Merge pull request #4683 from cloud-gov/chore-remove-site-users
Browse files Browse the repository at this point in the history
Remove SiteUsers
  • Loading branch information
drewbo authored Dec 6, 2024
2 parents 1ea4031 + b8fb0d9 commit e3d8f75
Show file tree
Hide file tree
Showing 63 changed files with 1,243 additions and 5,144 deletions.
8 changes: 0 additions & 8 deletions admin-client/src/pages/Site.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
fetchTasks,
fetchBuildTaskTypes,
fetchUserEnvironmentVariables,
fetchUsers,
updateSite,
addSiteBuildTask,
updateSiteBuildTask,
Expand All @@ -29,7 +28,6 @@
SiteFormWebhook,
SiteMetadata,
TaskTable,
UserTable,
} from '../components';
import { destroySite } from '../flows';
import { selectSiteDomains, stateColor } from '../lib/utils';
Expand All @@ -43,7 +41,6 @@
$: buildTasksPromise = fetchTasks({ site: id, limit: 10 });
$: buildTaskTypesPromise = fetchBuildTaskTypes();
$: orgsPromise = fetchOrganizations({ limit: 100 });
$: usersPromise = fetchUsers({ site: id });
$: uevsPromise = fetchUserEnvironmentVariables({ site: id });
const initEditedBuildTask = {
Expand Down Expand Up @@ -308,11 +305,6 @@
<TaskTable tasks={buildTasks.data} borderless={true} />
</Await>
</AccordionContent>
<AccordionContent title="Collaborators">
<Await on={usersPromise} let:response={users}>
<UserTable users={users.data} borderless={true} />
</Await>
</AccordionContent>
<AccordionContent title="Delete Site">
<SiteDeleteForm {site} on:submit={destroySite(site)} />
</AccordionContent>
Expand Down
10 changes: 6 additions & 4 deletions api/admin/controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ module.exports = wrapHandlers({
params: { id },
} = req;

const site = await Site.withUsers(id);
const hook = await GithubBuildHelper.createSiteWebhook(site, site.Users);
const site = await Site.findOne({ where: { id } });
const users = site.getOrgUsers();
const hook = await GithubBuildHelper.createSiteWebhook(site, users);

return res.json(hook || []);
},
Expand All @@ -80,8 +81,9 @@ module.exports = wrapHandlers({
params: { id },
} = req;

const site = await Site.withUsers(id);
const hooks = await GithubBuildHelper.listSiteWebhooks(site, site.Users);
const site = await Site.findOne({ where: { id } });
const users = site.getOrgUsers();
const hooks = await GithubBuildHelper.listSiteWebhooks(site, users);

return res.json(hooks);
},
Expand Down
6 changes: 1 addition & 5 deletions api/admin/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = wrapHandlers({
},

async list(req, res) {
const { limit, page, organization, search, site } = req.query;
const { limit, page, organization, search } = req.query;

const serialize = (users) => userSerializer.serializeMany(users, true);

Expand All @@ -90,10 +90,6 @@ module.exports = wrapHandlers({
scopes.push(User.searchScope(search));
}

if (site) {
scopes.push(User.siteScope(site));
}

if (organization) {
scopes.push(User.orgScope(organization));
scopes.push('withOrganizationRoles');
Expand Down
6 changes: 0 additions & 6 deletions api/authorizers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ const create = async (user, siteParams) => {
}
};

const addUser = () => Promise.resolve();

const createBuild = (user, site) => authorize(user, site);

const showActions = (user, site) => authorize(user, site);
Expand All @@ -114,15 +112,11 @@ const update = (user, site) => authorize(user, site);
const destroy = (user, site) =>
authorize(user, site).then(() => authorizeRepositoryAdmin(user, site));

const removeUser = (user, site) => authorize(user, site);

module.exports = {
create,
findOne,
update,
destroy,
addUser,
removeUser,
showActions,
createBuild,
};
36 changes: 4 additions & 32 deletions api/controllers/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const siteAuthorizer = require('../authorizers/site');
const SocketIOSubscriber = require('../services/SocketIOSubscriber');
const EventCreator = require('../services/EventCreator');
const { wrapHandlers } = require('../utils');
const { Build, Domain, User, Event, Site, SiteBranchConfig } = require('../models');
const { Build, Domain, Event, Site, SiteBranchConfig } = require('../models');
const { getSocket } = require('../socketIO');

const decodeb64 = (str) => Buffer.from(str, 'base64').toString('utf8');
Expand Down Expand Up @@ -89,16 +89,7 @@ module.exports = wrapHandlers({
id: req.body.buildId,
site: req.body.siteId,
},
include: [
{
model: Site,
include: [
{
model: User,
},
],
},
],
include: Site,
});

if (!requestBuild) {
Expand Down Expand Up @@ -134,20 +125,7 @@ module.exports = wrapHandlers({
async status(req, res) {
const { params, body } = req;

const build = await fetchModelById(params.id, Build, {
include: [
{
model: Site,
include: [
{
model: User,
},
Domain,
SiteBranchConfig,
],
},
],
});
const build = await fetchModelById(params.id, Build, { include: Site });

if (!build) {
return res.notFound();
Expand Down Expand Up @@ -189,13 +167,7 @@ module.exports = wrapHandlers({
include: [
{
model: Site,
include: [
{
model: User,
},
Domain,
SiteBranchConfig,
],
include: [Domain, SiteBranchConfig],
},
],
});
Expand Down
125 changes: 2 additions & 123 deletions api/controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,24 @@ const _ = require('underscore');
const authorizer = require('../authorizers/site');
const SiteCreator = require('../services/SiteCreator');
const SiteDestroyer = require('../services/SiteDestroyer');
const SiteMembershipCreator = require('../services/SiteMembershipCreator');
const UserActionCreator = require('../services/UserActionCreator');
const EventCreator = require('../services/EventCreator');
const siteSerializer = require('../serializers/site');
const domainSerializer = require('../serializers/domain');
const buildTaskSerializer = require('../serializers/build-task');
const {
Organization,
Site,
User,
Event,
Domain,
SiteBranchConfig,
SiteBuildTask,
BuildTaskType,
BuildTask,
} = require('../models');
const siteErrors = require('../responses/siteErrors');
const {
ValidationError,
validBasicAuthUsername,
validBasicAuthPassword,
} = require('../utils/validators');

const { toInt, wrapHandlers, appMatch } = require('../utils');
const { fetchModelById } = require('../utils/queryDatabase');

const stripCredentials = ({ username, password }) => {
if (validBasicAuthUsername(username) && validBasicAuthPassword(password)) {
return {
username,
password,
};
}

throw new ValidationError('username or password is not valid.');
};

module.exports = wrapHandlers({
async findAllForUser(req, res) {
const { user } = req;
Expand Down Expand Up @@ -99,69 +80,6 @@ module.exports = wrapHandlers({
return res.json({});
},

async addUser(req, res) {
const { body, user } = req;
if (!body.owner || !body.repository) {
return res.badRequest();
}

await authorizer.addUser(user, body);
const site = await SiteMembershipCreator.createSiteMembership({
user,
siteParams: body,
});
EventCreator.audit(Event.labels.USER_ACTION, req.user, 'SiteUser Created', {
siteUser: {
siteId: site.id,
userId: user.id,
},
});
const siteJSON = await siteSerializer.serialize(site);
return res.json(siteJSON);
},

async removeUser(req, res) {
const siteId = Number(req.params.site_id);
const userId = Number(req.params.user_id);

if (_.isNaN(siteId) || _.isNaN(userId)) {
return res.badRequest();
}

const site = await Site.withUsers(siteId);
if (!site) {
return res.notFound();
}

if (site.Users.length === 1) {
return res.badRequest({
message: siteErrors.USER_REQUIRED,
});
}

await authorizer.removeUser(req.user, site);
await SiteMembershipCreator.revokeSiteMembership({
user: req.user,
site,
userId,
});
EventCreator.audit(Event.labels.USER_ACTION, req.user, 'SiteUser Removed', {
siteUser: {
siteId: site.id,
userId,
},
});
// UserActionCreator to be deleted
await UserActionCreator.addRemoveAction({
userId: req.user.id,
targetId: userId,
targetType: 'user',
siteId: site.id,
});
const siteJSON = await siteSerializer.serialize(site);
return res.json(siteJSON);
},

async create(req, res) {
const {
body: { owner, template, organizationId, repository, engine },
Expand All @@ -185,7 +103,7 @@ module.exports = wrapHandlers({
site,
});
await site.reload({
include: [Organization, User],
include: [Organization],
});
const siteJSON = siteSerializer.serializeNew(site);
return res.json(siteJSON);
Expand Down Expand Up @@ -225,45 +143,6 @@ module.exports = wrapHandlers({
return res.json(siteJSON);
},

async addBasicAuth(req, res) {
const { body, params, user } = req;

const { site_id: siteId } = params;

const site = await fetchModelById(siteId, Site.forUser(user));

if (!site) {
return res.notFound();
}

const credentials = stripCredentials(body);

await site.update({
basicAuth: credentials,
});

const siteJSON = siteSerializer.serializeNew(site);
return res.json(siteJSON);
},

async removeBasicAuth(req, res) {
const { params, user } = req;
const { site_id: siteId } = params;

const site = await fetchModelById(siteId, Site.forUser(user));

if (!site) {
return res.notFound();
}

await site.update({
basicAuth: {},
});

const siteJSON = siteSerializer.serializeNew(site);
return res.json(siteJSON);
},

async getSiteDomains(req, res) {
const {
user,
Expand Down
Loading

0 comments on commit e3d8f75

Please sign in to comment.