-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: security and i18n installation.
- Loading branch information
Showing
20 changed files
with
428 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { install, isInstalled } from "./resolvers/install"; | ||
|
||
export default { | ||
/* GraphQL */ | ||
typeDefs: ` | ||
input I18NInstallInput { | ||
code: String! | ||
} | ||
extend type I18NQuery { | ||
"Is I18N installed?" | ||
isInstalled: I18NBooleanResponse | ||
} | ||
extend type I18NMutation { | ||
"Install I18N" | ||
install(data: I18NInstallInput!): I18NBooleanResponse | ||
} | ||
`, | ||
resolvers: { | ||
I18NQuery: { | ||
isInstalled | ||
}, | ||
I18NMutation: { | ||
install | ||
} | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/api-i18n/src/plugins/graphql/resolvers/install.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ErrorResponse, Response } from "@webiny/api"; | ||
import { WithFieldsError } from "@webiny/commodo"; | ||
import { InvalidFieldsError } from "@webiny/commodo-graphql"; | ||
|
||
export const install = async (root: any, args: Object, context: Object) => { | ||
const { I18NLocale } = context.models; | ||
|
||
try { | ||
const defaultLocale = new I18NLocale(); | ||
defaultLocale.code = args.data.code; | ||
defaultLocale.default = true; | ||
await defaultLocale.save(); | ||
} catch (e) { | ||
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) { | ||
const attrError = InvalidFieldsError.from(e); | ||
return new ErrorResponse({ | ||
code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS, | ||
message: attrError.message, | ||
data: attrError.data | ||
}); | ||
} | ||
return new ErrorResponse({ | ||
code: e.code, | ||
message: e.message, | ||
data: e.data | ||
}); | ||
} | ||
|
||
return new Response(true); | ||
}; | ||
|
||
export const isInstalled = async (root: any, args: Object, context: Object) => { | ||
const { I18NLocale } = context.models; | ||
|
||
// Check if at least 1 user exists in the system | ||
const localeCount = await I18NLocale.count(); | ||
|
||
return new Response(localeCount > 0); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,22 @@ Or if you prefer yarn: | |
``` | ||
yarn add @webiny/api-security | ||
``` | ||
|
||
## Security installation | ||
Installation mutation can be executed when certain conditions are met: | ||
- there must be no existing users in the Webiny DB, or... | ||
- there must be no existing users in the 3rd party auth provider, or... | ||
- all of the above | ||
|
||
If the above conditions are met, you can execute an `install` mutation | ||
to create a new user with `full-access` role (a root user). | ||
|
||
The logic behind user creation is built with the following scenarios in mind. | ||
Say you want to create a new user with `[email protected]` email: | ||
1) if a matching user is NOT FOUND in the Webiny DB, but is FOUND in auth provider, | ||
a new local user is created. Auth provider user remains intact. | ||
2) if a matching user is FOUND in the Webiny DB but is NOT FOUND in auth provider, | ||
a new user is created on your auth provider, and the local user's data is updated | ||
with the new firstName/lastName. | ||
3) if a matching user is NOT FOUND anywhere, a new user is first created in the Webiny DB, | ||
and after that, a new user is created in your auth provider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.