Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement organization creation permission controls #1373

Merged
merged 6 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fifty-eels-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': minor
'@clerk/types': minor
---

If user does not have permission to create an org, create org button will not display in the OrganizationSwitcher UI
3 changes: 3 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class User extends BaseResource implements UserResource {
publicMetadata: UserPublicMetadata = {};
unsafeMetadata: UserUnsafeMetadata = {};
lastSignInAt: Date | null = null;
createOrganizationEnabled = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we actually default this to false?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't matter in this case as a User is always initialized from json data.

You definitely have more context than me,but this seems fine!

updatedAt: Date | null = null;
createdAt: Date | null = null;

Expand Down Expand Up @@ -319,6 +320,8 @@ export class User extends BaseResource implements UserResource {
this.backupCodeEnabled = data.backup_code_enabled;
this.twoFactorEnabled = data.two_factor_enabled;

this.createOrganizationEnabled = data.create_organization_enabled;

if (data.last_sign_in_at) {
this.lastSignInAt = unixEpochToDate(data.last_sign_in_at);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/clerk-js/src/core/resources/UserSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
Attributes,
Actions,
OAuthProviders,
OAuthStrategy,
PasswordSettingsData,
Expand All @@ -17,6 +16,11 @@ import { BaseResource } from './internal';
const defaultMaxPasswordLength = 72;
const defaultMinPasswordLength = 8;

export type Actions = {
create_organization: boolean;
delete_self: boolean;
};

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const OrganizationActionList = (props: OrganizationActionListProps) => {
</PreviewButton>
))}
</Box>
{createOrganizationButton}
{user.createOrganizationEnabled && createOrganizationButton}
</SecondaryActions>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('OrganizationSwitcher', () => {
it('opens the organization switcher popover when clicked', async () => {
const { wrapper, props } = await createFixtures(f => {
f.withOrganizations();
f.withUser({ email_addresses: ['test@clerk.dev'] });
f.withUser({ email_addresses: ['test@clerk.dev'], create_organization_enabled: true });
});
props.setProps({ hidePersonal: true });
const { getByText, getByRole, userEvent } = render(<OrganizationSwitcher />, { wrapper });
Expand Down Expand Up @@ -107,6 +107,7 @@ describe('OrganizationSwitcher', () => {
f.withUser({
email_addresses: ['test@clerk.dev'],
organization_memberships: [{ name: 'Org1', role: 'basic_member' }],
create_organization_enabled: true,
});
});
props.setProps({ hidePersonal: true });
Expand All @@ -116,6 +117,20 @@ describe('OrganizationSwitcher', () => {
expect(fixtures.clerk.openCreateOrganization).toHaveBeenCalled();
});

it('does not display create organization button if permissions not present', async () => {
const { wrapper, props } = await createFixtures(f => {
f.withOrganizations();
f.withUser({
email_addresses: ['test@clerk.dev'],
organization_memberships: [{ name: 'Org1', role: 'basic_member' }],
create_organization_enabled: false,
});
});
props.setProps({ hidePersonal: true });
const { queryByRole } = render(<OrganizationSwitcher />, { wrapper });
expect(queryByRole('button', { name: 'Create Organization' })).not.toBeInTheDocument();
});

it.todo('switches between active organizations when one is clicked');
});
});
1 change: 1 addition & 0 deletions packages/types/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export interface UserJSON extends ClerkResourceJSON {
public_metadata: UserPublicMetadata;
unsafe_metadata: UserUnsafeMetadata;
last_sign_in_at: number | null;
create_organization_enabled: boolean;
updated_at: number;
created_at: number;
}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface UserResource extends ClerkResource {
publicMetadata: UserPublicMetadata;
unsafeMetadata: UserUnsafeMetadata;
lastSignInAt: Date | null;
createOrganizationEnabled: boolean;
updatedAt: Date | null;
createdAt: Date | null;

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface UserSettingsJSON extends ClerkResourceJSON {
id: never;
object: never;
attributes: AttributesJSON;
actions: Actions;
social: OAuthProviders;

/**
Expand All @@ -101,7 +102,6 @@ export interface UserSettingsJSON extends ClerkResourceJSON {
sign_in: SignInData;
sign_up: SignUpData;
password_settings: PasswordSettingsData;
actions: Actions;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a move to make the placement consistent between the resource and json types

}

export interface UserSettingsResource extends ClerkResource {
Expand Down