Skip to content

Commit

Permalink
Merge pull request #1147 from amazeeio/1145
Browse files Browse the repository at this point in the history
Adding the DeleteSshKeyById API endpoint.
  • Loading branch information
Schnitzel committed Nov 20, 2019
2 parents 69fa155 + cafa3b9 commit cab978d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions node-packages/commons/src/api.js
Expand Up @@ -489,6 +489,20 @@ const deleteSshKey = (name: string): Promise<Object> =>
},
);

const deleteSshKeyById = (id: number): Promise<Object> =>
graphqlapi.mutate(
`
($id: Int!) {
deleteSshKeyById(input: {
id: $id
})
}
`,
{
name,
},
);

const addProject = (
name: string,
gitUrl: string,
Expand Down
11 changes: 11 additions & 0 deletions services/api-db/docker-entrypoint-initdb.d/03-procedures.sql
Expand Up @@ -247,6 +247,17 @@ CREATE OR REPLACE PROCEDURE
END;
$$

CREATE OR REPLACE PROCEDURE
DeleteSshKeyById
(
IN s_id int
)
BEGIN
DELETE FROM user_ssh_key WHERE skid = s_id;
DELETE FROM ssh_key WHERE id = s_id;
END;
$$

CREATE OR REPLACE PROCEDURE
CreateOpenshift
(
Expand Down
2 changes: 2 additions & 0 deletions services/api/src/resolvers.js
Expand Up @@ -99,6 +99,7 @@ const {
addSshKey,
updateSshKey,
deleteSshKey,
deleteSshKeyById,
deleteAllSshKeys,
removeAllSshKeysFromAllUsers,
} = require('./resources/sshKey/resolvers');
Expand Down Expand Up @@ -246,6 +247,7 @@ const resolvers /* : { [string]: ResolversObj | typeof GraphQLDate } */ = {
addSshKey,
updateSshKey,
deleteSshKey,
deleteSshKeyById,
deleteAllSshKeys,
removeAllSshKeysFromAllUsers,
addUser,
Expand Down
19 changes: 19 additions & 0 deletions services/api/src/resources/sshKey/resolvers.js
Expand Up @@ -159,6 +159,24 @@ const deleteSshKey = async (
return 'success';
};

const deleteSshKeyById = async (
root,
{ input: { id } },
{ sqlClient, hasPermission },
) => {
const perms = await query(sqlClient, Sql.selectUserIdsBySshKeyId(id));
const userIds = R.map(R.prop('usid'), perms);

await hasPermission('ssh_key', 'delete', {
users: userIds.join(','),
});

const prep = prepare(sqlClient, 'CALL DeleteSshKeyById(:id)');
await query(sqlClient, prep({ id }));

return 'success';
};

const deleteAllSshKeys = async (
root,
args,
Expand Down Expand Up @@ -190,6 +208,7 @@ const Resolvers /* : ResolversObj */ = {
addSshKey,
updateSshKey,
deleteSshKey,
deleteSshKeyById,
deleteAllSshKeys,
removeAllSshKeysFromAllUsers,
};
Expand Down
5 changes: 5 additions & 0 deletions services/api/src/typeDefs.js
Expand Up @@ -524,6 +524,10 @@ const typeDefs = gql`
name: String!
}
input DeleteSshKeyByIdInput {
id: Int!
}
input AddProjectInput {
id: Int
name: String!
Expand Down Expand Up @@ -976,6 +980,7 @@ const typeDefs = gql`
addSshKey(input: AddSshKeyInput!): SshKey
updateSshKey(input: UpdateSshKeyInput!): SshKey
deleteSshKey(input: DeleteSshKeyInput!): String
deleteSshKeyById(input: DeleteSshKeyByIdInput!): String
deleteAllSshKeys: String
removeAllSshKeysFromAllUsers: String
addUser(input: AddUserInput!): User
Expand Down

0 comments on commit cab978d

Please sign in to comment.