-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [NEW] Prevent users from accidentally deactivating an enterprise license by adding more users than the license allows. * Seats cap banners * Deprecate preserveDismiss * use request seats link * Fix banner not closing and request seats link Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat>
- Loading branch information
1 parent
e530fae
commit 3818eeb
Showing
11 changed files
with
260 additions
and
10 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 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,106 @@ | ||
import { BlockType } from '@rocket.chat/apps-engine/definition/uikit/blocks/Blocks'; | ||
import { TextObjectType } from '@rocket.chat/apps-engine/definition/uikit/blocks/Objects'; | ||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; | ||
|
||
import { IBanner, BannerPlatform } from '../../../../definition/IBanner'; | ||
import { Banner } from '../../../../server/sdk'; | ||
import { getSeatsRequestLink } from './getSeatsRequestLink'; | ||
|
||
const WARNING_BANNER_ID = 'closeToSeatsLimit'; | ||
const DANGER_BANNER_ID = 'reachedSeatsLimit'; | ||
|
||
|
||
const makeWarningBanner = (seats: number): IBanner => ({ | ||
_id: WARNING_BANNER_ID, | ||
platform: [BannerPlatform.Web], | ||
roles: ['admin'], | ||
view: { | ||
icon: 'warning', | ||
variant: 'warning', | ||
viewId: '', | ||
appId: 'banner-core', | ||
blocks: [{ | ||
type: BlockType.SECTION, | ||
blockId: 'attention', | ||
text: { | ||
type: TextObjectType.MARKDOWN, | ||
text: TAPi18n.__('Close_to_seat_limit_banner_warning', { seats, url: getSeatsRequestLink() }), | ||
emoji: false, | ||
}, | ||
}], | ||
}, | ||
createdBy: { | ||
_id: 'rocket.cat', | ||
username: 'rocket.cat', | ||
}, | ||
expireAt: new Date(8640000000000000), | ||
startAt: new Date(), | ||
createdAt: new Date(), | ||
_updatedAt: new Date(), | ||
active: false, | ||
}); | ||
|
||
const makeDangerBanner = (): IBanner => ({ | ||
_id: DANGER_BANNER_ID, | ||
platform: [BannerPlatform.Web], | ||
roles: ['admin'], | ||
view: { | ||
icon: 'ban', | ||
variant: 'danger', | ||
viewId: '', | ||
appId: 'banner-core', | ||
blocks: [{ | ||
type: BlockType.SECTION, | ||
blockId: 'attention', | ||
text: { | ||
type: TextObjectType.MARKDOWN, | ||
text: TAPi18n.__('Reached_seat_limit_banner_warning', { url: getSeatsRequestLink() }), | ||
emoji: false, | ||
}, | ||
}], | ||
}, | ||
createdBy: { | ||
_id: 'rocket.cat', | ||
username: 'rocket.cat', | ||
}, | ||
expireAt: new Date(8640000000000000), | ||
startAt: new Date(), | ||
createdAt: new Date(), | ||
_updatedAt: new Date(), | ||
active: false, | ||
}); | ||
|
||
export const createSeatsLimitBanners = async (): Promise<void> => { | ||
const [warning, danger] = await Promise.all([Banner.getById(WARNING_BANNER_ID), Banner.getById(DANGER_BANNER_ID)]); | ||
if (!warning) { | ||
Banner.create(makeWarningBanner(0)); | ||
} | ||
if (!danger) { | ||
Banner.create(makeDangerBanner()); | ||
} | ||
}; | ||
|
||
export const enableDangerBanner = (): void => { | ||
Banner.enable(DANGER_BANNER_ID, makeDangerBanner()); | ||
}; | ||
|
||
|
||
export const disableDangerBannerDiscardingDismissal = async (): Promise<void> => { | ||
const banner = await Banner.getById(DANGER_BANNER_ID); | ||
if (banner && banner.active) { | ||
Banner.disable(DANGER_BANNER_ID); | ||
Banner.discardDismissal(DANGER_BANNER_ID); | ||
} | ||
}; | ||
|
||
export const enableWarningBanner = (seatsLeft: number): void => { | ||
Banner.enable(WARNING_BANNER_ID, makeWarningBanner(seatsLeft)); | ||
}; | ||
|
||
export const disableWarningBannerDiscardingDismissal = async (): Promise<void> => { | ||
const banner = await Banner.getById(WARNING_BANNER_ID); | ||
if (banner && banner.active) { | ||
Banner.disable(WARNING_BANNER_ID); | ||
Banner.discardDismissal(WARNING_BANNER_ID); | ||
} | ||
}; |
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 @@ | ||
import './seatsCap'; |
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,76 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; | ||
|
||
import { callbacks } from '../../../app/callbacks/server'; | ||
import { canAddNewUser, handleMaxSeatsBanners, isEnterprise } from '../../app/license/server/license'; | ||
import { createSeatsLimitBanners } from '../../app/license/server/maxSeatsBanners'; | ||
import { validateUserRoles } from '../../app/authorization/server/validateUserRoles'; | ||
import { Users } from '../../../app/models/server'; | ||
import type { IUser } from '../../../definition/IUser'; | ||
|
||
callbacks.add('onCreateUser', ({ isGuest }: { isGuest: boolean }) => { | ||
if (isGuest) { | ||
return; | ||
} | ||
|
||
if (!canAddNewUser()) { | ||
throw new Meteor.Error('error-license-user-limit-reached', TAPi18n.__('error-license-user-limit-reached')); | ||
} | ||
}, callbacks.priority.MEDIUM, 'check-max-user-seats'); | ||
|
||
|
||
callbacks.add('beforeActivateUser', (user: IUser) => { | ||
if (user.roles.length === 1 && user.roles.includes('guest')) { | ||
return; | ||
} | ||
|
||
if (user.type === 'app') { | ||
return; | ||
} | ||
|
||
if (!canAddNewUser()) { | ||
throw new Meteor.Error('error-license-user-limit-reached', TAPi18n.__('error-license-user-limit-reached')); | ||
} | ||
}, callbacks.priority.MEDIUM, 'check-max-user-seats'); | ||
|
||
callbacks.add('validateUserRoles', (userData: Record<string, any>) => { | ||
const isGuest = userData.roles?.includes('guest'); | ||
if (isGuest) { | ||
validateUserRoles(Meteor.userId(), userData); | ||
return; | ||
} | ||
|
||
if (!userData._id) { | ||
return; | ||
} | ||
|
||
const currentUserData = Users.findOneById(userData._id); | ||
if (currentUserData.type === 'app') { | ||
return; | ||
} | ||
|
||
const wasGuest = currentUserData?.roles?.length === 1 && currentUserData.roles.includes('guest'); | ||
if (!wasGuest) { | ||
return; | ||
} | ||
|
||
if (!canAddNewUser()) { | ||
throw new Meteor.Error('error-license-user-limit-reached', TAPi18n.__('error-license-user-limit-reached')); | ||
} | ||
}, callbacks.priority.MEDIUM, 'check-max-user-seats'); | ||
|
||
callbacks.add('afterCreateUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); | ||
|
||
callbacks.add('afterSaveUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); | ||
|
||
callbacks.add('afterDeleteUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); | ||
|
||
callbacks.add('afterDeactivateUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); | ||
|
||
callbacks.add('afterActivateUser', handleMaxSeatsBanners, callbacks.priority.MEDIUM, 'handle-max-seats-banners'); | ||
|
||
Meteor.startup(() => { | ||
if (isEnterprise()) { | ||
createSeatsLimitBanners(); | ||
} | ||
}); |
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