chore: update license service

This commit is contained in:
Steven 2024-08-12 21:17:27 +08:00
parent 768af5b096
commit 00e2a6fd96
13 changed files with 297 additions and 107 deletions

View File

@ -1,11 +1,20 @@
import { Tooltip } from "@mui/joy";
import { FeatureType, checkFeatureAvailable } from "@/helpers/feature";
import { useWorkspaceStore } from "@/stores";
import Icon from "./Icon";
interface Props {
feature: FeatureType;
className?: string;
}
const FeatureBadge = ({ className }: Props) => {
const FeatureBadge = ({ feature, className }: Props) => {
const workspaceStore = useWorkspaceStore();
const isFeatureEnabled = checkFeatureAvailable(feature, workspaceStore.profile.plan);
if (isFeatureEnabled) {
return null;
}
return (
<Tooltip title="This feature is not available on your plan." className={className} placement="top" arrow>
<Icon.Sparkles />

View File

@ -67,20 +67,20 @@ const MemberSection = () => {
</div>
<div className="mt-2 flow-root">
<div className="overflow-x-auto">
<div className="inline-block min-w-full py-2 align-middle">
<div className="inline-block border rounded-lg border-gray-300 dark:border-zinc-700 min-w-full align-middle">
<table className="min-w-full divide-y divide-gray-300 dark:divide-zinc-700">
<thead>
<tr>
<th scope="col" className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
<th scope="col" className="py-3 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
{t("user.nickname")}
</th>
<th scope="col" className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
<th scope="col" className="px-3 py-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
{t("user.email")}
</th>
<th scope="col" className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
<th scope="col" className="px-3 py-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
{t("user.role")}
</th>
<th scope="col" className="relative py-3.5 pl-3 pr-4">
<th scope="col" className="relative py-3 pl-3 pr-4">
<span className="sr-only">{t("common.edit")}</span>
</th>
</tr>
@ -88,10 +88,10 @@ const MemberSection = () => {
<tbody className="divide-y divide-gray-200 dark:divide-zinc-800">
{userList.map((user) => (
<tr key={user.email}>
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-500">{user.nickname}</td>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{user.email}</td>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{convertRoleFromPb(user.role)}</td>
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm">
<td className="whitespace-nowrap py-2 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-500">{user.nickname}</td>
<td className="whitespace-nowrap px-3 py-2 text-sm text-gray-500">{user.email}</td>
<td className="whitespace-nowrap px-3 py-2 text-sm text-gray-500">{convertRoleFromPb(user.role)}</td>
<td className="relative whitespace-nowrap py-2 pl-3 pr-4 text-right text-sm">
<IconButton
size="sm"
variant="plain"

View File

@ -3,9 +3,11 @@ import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { workspaceServiceClient } from "@/grpcweb";
import { checkFeatureAvailable, FeatureType } from "@/helpers/feature";
import { useWorkspaceStore } from "@/stores";
import { IdentityProvider } from "@/types/proto/api/v1/workspace_service";
import CreateIdentityProviderDrawer from "../CreateIdentityProviderDrawer";
import FeatureBadge from "../FeatureBadge";
import Icon from "../Icon";
interface EditState {
@ -19,6 +21,7 @@ const SSOSection = () => {
const workspaceStore = useWorkspaceStore();
const [identityProviderList, setIdentityProviderList] = useState<IdentityProvider[]>([]);
const [editState, setEditState] = useState<EditState>({ open: false, identityProvider: undefined });
const isSSOFeatureEnabled = checkFeatureAvailable(FeatureType.SSO, workspaceStore.profile.plan);
useEffect(() => {
fetchIdentityProviderList();
@ -53,10 +56,12 @@ const SSOSection = () => {
<div className="w-full flex flex-row justify-between items-center gap-1">
<div className="flex flex-row items-center gap-1">
<span className="font-medium dark:text-gray-400">SSO</span>
<FeatureBadge className="w-5 h-auto ml-1 text-blue-600" feature={FeatureType.SSO} />
</div>
<Button
variant="outlined"
color="neutral"
disabled={!isSSOFeatureEnabled}
onClick={() =>
setEditState({
open: true,
@ -70,17 +75,17 @@ const SSOSection = () => {
{identityProviderList.length > 0 && (
<div className="mt-2 flow-root">
<div className="overflow-x-auto">
<div className="inline-block min-w-full py-2 align-middle">
<div className="inline-block border rounded-lg border-gray-300 dark:border-zinc-700 min-w-full align-middle">
<table className="min-w-full divide-y divide-gray-300 dark:divide-zinc-700">
<thead>
<tr>
<th scope="col" className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
<th scope="col" className="py-2 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
ID
</th>
<th scope="col" className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
<th scope="col" className="px-3 py-2 text-left text-sm font-semibold text-gray-900 dark:text-gray-500">
Title
</th>
<th scope="col" className="relative py-3.5 pl-3 pr-4">
<th scope="col" className="relative py-2 pl-3 pr-4">
<span className="sr-only">{t("common.edit")}</span>
</th>
</tr>
@ -88,11 +93,11 @@ const SSOSection = () => {
<tbody className="divide-y divide-gray-200 dark:divide-zinc-800">
{identityProviderList.map((identityProvider) => (
<tr key={identityProvider.name}>
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-500">
<td className="whitespace-nowrap py-2 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-500">
{identityProvider.name}
</td>
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{identityProvider.title}</td>
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm">
<td className="whitespace-nowrap px-3 py-2 text-sm text-gray-500">{identityProvider.title}</td>
<td className="relative whitespace-nowrap py-2 pl-3 pr-4 text-right text-sm">
<IconButton
size="sm"
variant="plain"

View File

@ -4,6 +4,7 @@ import { useRef, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { workspaceServiceClient } from "@/grpcweb";
import { FeatureType } from "@/helpers/feature";
import { useWorkspaceStore } from "@/stores";
import { Visibility } from "@/types/proto/api/v1/common";
import { PlanType } from "@/types/proto/api/v1/subscription_service";
@ -102,7 +103,7 @@ const WorkspaceSection = () => {
<div className="w-full flex flex-col justify-start items-start">
<p className="flex flex-row justify-start items-center">
<span className="font-medium dark:text-gray-400">Custom branding</span>
<FeatureBadge className="w-5 h-auto ml-1 text-blue-600" />
<FeatureBadge className="w-5 h-auto ml-1 text-blue-600" feature={FeatureType.CustomeBranding} />
</p>
<p className="text-sm text-gray-500 leading-tight">Recommand logo ratio: 1:1</p>
</div>

View File

@ -0,0 +1,51 @@
// const (
// // Enterprise features.
import { PlanType } from "@/types/proto/api/v1/subscription_service";
// // FeatureTypeSSO allows the user to use SSO.
// FeatureTypeSSO FeatureType = "ysh.slash.sso"
// // FeatureTypeAdvancedAnalytics allows the user to use advanced analytics.
// FeatureTypeAdvancedAnalytics FeatureType = "ysh.slash.advanced-analytics"
// // Usages.
// // FeatureTypeUnlimitedAccounts allows the user to create unlimited accounts.
// FeatureTypeUnlimitedAccounts FeatureType = "ysh.slash.unlimited-accounts"
// // FeatureTypeUnlimitedAccounts allows the user to create unlimited collections.
// FeatureTypeUnlimitedCollections FeatureType = "ysh.slash.unlimited-collections"
// // Customization.
// // FeatureTypeCustomeBranding allows the user to customize the branding.
// FeatureTypeCustomeBranding FeatureType = "ysh.slash.custom-branding"
// )
export enum FeatureType {
SSO = "ysh.slash.sso",
AdvancedAnalytics = "ysh.slash.advanced-analytics",
UnlimitedAccounts = "ysh.slash.unlimited-accounts",
UnlimitedCollections = "ysh.slash.unlimited-collections",
CustomeBranding = "ysh.slash.custom-branding",
}
const FeatureMatrix: Record<FeatureType, [boolean, boolean, boolean]> = {
[FeatureType.SSO]: [false, false, true],
[FeatureType.AdvancedAnalytics]: [false, false, true],
[FeatureType.UnlimitedAccounts]: [false, true, false],
[FeatureType.UnlimitedCollections]: [false, true, true],
[FeatureType.CustomeBranding]: [false, true, true],
};
export const checkFeatureAvailable = (feature: FeatureType, plan: PlanType): boolean => {
const [isFree, isPro, isEnterprise] = FeatureMatrix[feature];
switch (plan) {
case PlanType.FREE:
return isFree;
case PlanType.PRO:
return isPro;
case PlanType.ENTERPRISE:
return isEnterprise;
default:
return false;
}
};

View File

@ -26,12 +26,17 @@ message Subscription {
google.protobuf.Timestamp started_time = 2 [(google.api.field_behavior) = OUTPUT_ONLY];
google.protobuf.Timestamp expires_time = 3 [(google.api.field_behavior) = OUTPUT_ONLY];
int32 seats = 4 [(google.api.field_behavior) = OUTPUT_ONLY];
repeated string features = 5 [(google.api.field_behavior) = OUTPUT_ONLY];
}
enum PlanType {
PLAN_TYPE_UNSPECIFIED = 0;
FREE = 1;
PRO = 2;
ENTERPRISE = 3;
}
message GetSubscriptionRequest {}

View File

@ -1144,6 +1144,8 @@
| plan | [PlanType](#slash-api-v1-PlanType) | | |
| started_time | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
| expires_time | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
| seats | [int32](#int32) | | |
| features | [string](#string) | repeated | |
@ -1192,6 +1194,7 @@
| PLAN_TYPE_UNSPECIFIED | 0 | |
| FREE | 1 | |
| PRO | 2 | |
| ENTERPRISE | 3 | |

View File

@ -28,6 +28,7 @@ const (
PlanType_PLAN_TYPE_UNSPECIFIED PlanType = 0
PlanType_FREE PlanType = 1
PlanType_PRO PlanType = 2
PlanType_ENTERPRISE PlanType = 3
)
// Enum value maps for PlanType.
@ -36,11 +37,13 @@ var (
0: "PLAN_TYPE_UNSPECIFIED",
1: "FREE",
2: "PRO",
3: "ENTERPRISE",
}
PlanType_value = map[string]int32{
"PLAN_TYPE_UNSPECIFIED": 0,
"FREE": 1,
"PRO": 2,
"ENTERPRISE": 3,
}
)
@ -79,6 +82,8 @@ type Subscription struct {
Plan PlanType `protobuf:"varint,1,opt,name=plan,proto3,enum=slash.api.v1.PlanType" json:"plan,omitempty"`
StartedTime *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=started_time,json=startedTime,proto3" json:"started_time,omitempty"`
ExpiresTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=expires_time,json=expiresTime,proto3" json:"expires_time,omitempty"`
Seats int32 `protobuf:"varint,4,opt,name=seats,proto3" json:"seats,omitempty"`
Features []string `protobuf:"bytes,5,rep,name=features,proto3" json:"features,omitempty"`
}
func (x *Subscription) Reset() {
@ -134,6 +139,20 @@ func (x *Subscription) GetExpiresTime() *timestamppb.Timestamp {
return nil
}
func (x *Subscription) GetSeats() int32 {
if x != nil {
return x.Seats
}
return 0
}
func (x *Subscription) GetFeatures() []string {
if x != nil {
return x.Features
}
return nil
}
type GetSubscriptionRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -325,7 +344,7 @@ var file_api_v1_subscription_service_proto_rawDesc = []byte{
0x64, 0x5f, 0x62, 0x65, 0x68, 0x61, 0x76, 0x69, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,
0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x22, 0xca, 0x01, 0x0a, 0x0c, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69,
0x6f, 0x22, 0x88, 0x02, 0x0a, 0x0c, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69,
0x6f, 0x6e, 0x12, 0x30, 0x0a, 0x04, 0x70, 0x6c, 0x61, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e,
0x32, 0x16, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e,
0x50, 0x6c, 0x61, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x42, 0x04, 0xe2, 0x41, 0x01, 0x03, 0x52, 0x04,
@ -337,58 +356,63 @@ var file_api_v1_subscription_service_proto_rawDesc = []byte{
0x69, 0x72, 0x65, 0x73, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32,
0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,
0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x42, 0x04, 0xe2, 0x41, 0x01,
0x03, 0x52, 0x0b, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x54, 0x69, 0x6d, 0x65, 0x22, 0x18,
0x0a, 0x16, 0x47, 0x65, 0x74, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x59, 0x0a, 0x17, 0x47, 0x65, 0x74, 0x53,
0x03, 0x52, 0x0b, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1a,
0x0a, 0x05, 0x73, 0x65, 0x61, 0x74, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x42, 0x04, 0xe2,
0x41, 0x01, 0x03, 0x52, 0x05, 0x73, 0x65, 0x61, 0x74, 0x73, 0x12, 0x20, 0x0a, 0x08, 0x66, 0x65,
0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x42, 0x04, 0xe2, 0x41,
0x01, 0x03, 0x52, 0x08, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x22, 0x18, 0x0a, 0x16,
0x47, 0x65, 0x74, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x59, 0x0a, 0x17, 0x47, 0x65, 0x74, 0x53, 0x75, 0x62,
0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x3e, 0x0a, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e,
0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x69, 0x6f, 0x6e, 0x52, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x22, 0x42, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73, 0x63,
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x25,
0x0a, 0x0b, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x42, 0x04, 0xe2, 0x41, 0x01, 0x02, 0x52, 0x0a, 0x6c, 0x69, 0x63, 0x65, 0x6e,
0x73, 0x65, 0x4b, 0x65, 0x79, 0x22, 0x5c, 0x0a, 0x1a, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53,
0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x12, 0x3e, 0x0a, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x73, 0x6c, 0x61, 0x73,
0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69,
0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x69, 0x6f, 0x6e, 0x22, 0x42, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62,
0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x12, 0x25, 0x0a, 0x0b, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x04, 0xe2, 0x41, 0x01, 0x02, 0x52, 0x0a, 0x6c, 0x69, 0x63,
0x65, 0x6e, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x22, 0x5c, 0x0a, 0x1a, 0x55, 0x70, 0x64, 0x61, 0x74,
0x65, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3e, 0x0a, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69,
0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x73, 0x6c,
0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63,
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0c, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69,
0x70, 0x74, 0x69, 0x6f, 0x6e, 0x2a, 0x38, 0x0a, 0x08, 0x50, 0x6c, 0x61, 0x6e, 0x54, 0x79, 0x70,
0x65, 0x12, 0x19, 0x0a, 0x15, 0x50, 0x4c, 0x41, 0x4e, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55,
0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04,
0x46, 0x52, 0x45, 0x45, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x50, 0x52, 0x4f, 0x10, 0x02, 0x32,
0x96, 0x02, 0x0a, 0x13, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e,
0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x78, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x53, 0x75,
0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x24, 0x2e, 0x73, 0x6c, 0x61,
0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x75, 0x62,
0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x25, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e,
0x47, 0x65, 0x74, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x18, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x12, 0x12,
0x10, 0x2f, 0x76, 0x31, 0x2f, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x12, 0x84, 0x01, 0x0a, 0x12, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73,
0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x27, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68,
0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75,
0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x1a, 0x28, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31,
0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1b, 0x82, 0xd3, 0xe4,
0x93, 0x02, 0x15, 0x3a, 0x01, 0x2a, 0x32, 0x10, 0x2f, 0x76, 0x31, 0x2f, 0x73, 0x75, 0x62, 0x73,
0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0xb6, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d,
0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x42, 0x18, 0x53,
0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75,
0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x79, 0x6f, 0x75, 0x72, 0x73, 0x65, 0x6c, 0x66, 0x68, 0x6f,
0x73, 0x74, 0x65, 0x64, 0x2f, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x3b, 0x61, 0x70, 0x69, 0x76,
0x31, 0xa2, 0x02, 0x03, 0x53, 0x41, 0x58, 0xaa, 0x02, 0x0c, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x2e,
0x41, 0x70, 0x69, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x0c, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x5c, 0x41,
0x70, 0x69, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x5c, 0x41, 0x70,
0x69, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61,
0xea, 0x02, 0x0e, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x3a, 0x3a, 0x41, 0x70, 0x69, 0x3a, 0x3a, 0x56,
0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x69, 0x6f, 0x6e, 0x2a, 0x48, 0x0a, 0x08, 0x50, 0x6c, 0x61, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12,
0x19, 0x0a, 0x15, 0x50, 0x4c, 0x41, 0x4e, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x4e, 0x53,
0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x46, 0x52,
0x45, 0x45, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x50, 0x52, 0x4f, 0x10, 0x02, 0x12, 0x0e, 0x0a,
0x0a, 0x45, 0x4e, 0x54, 0x45, 0x52, 0x50, 0x52, 0x49, 0x53, 0x45, 0x10, 0x03, 0x32, 0x96, 0x02,
0x0a, 0x13, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x78, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x53, 0x75, 0x62, 0x73,
0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x24, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68,
0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x75, 0x62, 0x73, 0x63,
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x25,
0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65,
0x74, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x18, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x12, 0x12, 0x10, 0x2f,
0x76, 0x31, 0x2f, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12,
0x84, 0x01, 0x0a, 0x12, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72,
0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x27, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61,
0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73,
0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x28, 0x2e, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x55,
0x70, 0x64, 0x61, 0x74, 0x65, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1b, 0x82, 0xd3, 0xe4, 0x93, 0x02,
0x15, 0x3a, 0x01, 0x2a, 0x32, 0x10, 0x2f, 0x76, 0x31, 0x2f, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72,
0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0xb6, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x73,
0x6c, 0x61, 0x73, 0x68, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x42, 0x18, 0x53, 0x75, 0x62,
0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e,
0x63, 0x6f, 0x6d, 0x2f, 0x79, 0x6f, 0x75, 0x72, 0x73, 0x65, 0x6c, 0x66, 0x68, 0x6f, 0x73, 0x74,
0x65, 0x64, 0x2f, 0x73, 0x6c, 0x61, 0x73, 0x68, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67,
0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x3b, 0x61, 0x70, 0x69, 0x76, 0x31, 0xa2,
0x02, 0x03, 0x53, 0x41, 0x58, 0xaa, 0x02, 0x0c, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x41, 0x70,
0x69, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x0c, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x5c, 0x41, 0x70, 0x69,
0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x5c, 0x41, 0x70, 0x69, 0x5c,
0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02,
0x0e, 0x53, 0x6c, 0x61, 0x73, 0x68, 0x3a, 0x3a, 0x41, 0x70, 0x69, 0x3a, 0x3a, 0x56, 0x31, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -1100,6 +1100,7 @@ definitions:
- PLAN_TYPE_UNSPECIFIED
- FREE
- PRO
- ENTERPRISE
default: PLAN_TYPE_UNSPECIFIED
v1Role:
type: string
@ -1122,6 +1123,15 @@ definitions:
type: string
format: date-time
readOnly: true
seats:
type: integer
format: int32
readOnly: true
features:
type: array
items:
type: string
readOnly: true
v1UpdateCollectionResponse:
type: object
properties:

View File

@ -58,6 +58,10 @@ func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest)
}
func (s *APIV1Service) SignInWithSSO(ctx context.Context, request *v1pb.SignInWithSSORequest) (*v1pb.User, error) {
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeSSO) {
return nil, status.Errorf(codes.PermissionDenied, "SSO is not available in the current plan")
}
identityProviderSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_IDENTITY_PROVIDER,
})
@ -105,6 +109,9 @@ func (s *APIV1Service) SignInWithSSO(ctx context.Context, request *v1pb.SignInWi
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to find user by email %s", email))
}
if user == nil {
if err := s.checkSeatAvailability(ctx); err != nil {
return nil, err
}
userCreate := &store.User{
Email: email,
Nickname: userInfo.DisplayName,
@ -139,15 +146,8 @@ func (s *APIV1Service) SignUp(ctx context.Context, request *v1pb.SignUpRequest)
if !s.Profile.Public {
return nil, status.Errorf(codes.PermissionDenied, "sign up is not allowed")
}
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err))
}
if len(userList) >= 5 {
return nil, status.Errorf(codes.InvalidArgument, "maximum number of users reached")
}
if err := s.checkSeatAvailability(ctx); err != nil {
return nil, err
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(request.Password), bcrypt.DefaultCost)
@ -210,3 +210,17 @@ func (*APIV1Service) SignOut(ctx context.Context, _ *v1pb.SignOutRequest) (*empt
}
return &emptypb.Empty{}, nil
}
func (s *APIV1Service) checkSeatAvailability(ctx context.Context) error {
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err))
}
seats := s.LicenseService.GetSubscription().Seats
if len(userList) > int(seats) {
return status.Errorf(codes.FailedPrecondition, "maximum number of users %d reached", seats)
}
}
return nil
}

View File

@ -22,10 +22,7 @@ func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *v1pb.GetWorks
}
// Load subscription plan from license service.
subscription, err := s.LicenseService.GetSubscription(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get subscription: %v", err)
}
subscription := s.LicenseService.GetSubscription()
workspaceProfile.Plan = subscription.Plan
owner, err := s.GetInstanceOwner(ctx)
@ -53,6 +50,10 @@ func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *v1pb.GetWorks
}
func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *v1pb.GetWorkspaceSettingRequest) (*v1pb.GetWorkspaceSettingResponse, error) {
currentUser, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to get current user: %v", err)
}
workspaceSettings, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list workspace settings: %v", err)
@ -70,7 +71,14 @@ func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *v1pb.GetWorks
identityProviderSetting := v.GetIdentityProvider()
workspaceSetting.IdentityProviders = []*v1pb.IdentityProvider{}
for _, identityProvider := range identityProviderSetting.GetIdentityProviders() {
workspaceSetting.IdentityProviders = append(workspaceSetting.IdentityProviders, convertIdentityProviderFromStore(identityProvider))
identityProviderV1pb := convertIdentityProviderFromStore(identityProvider)
if currentUser == nil || currentUser.Role != store.RoleAdmin {
oauth2Config := identityProviderV1pb.Config.GetOauth2()
if oauth2Config != nil {
oauth2Config.ClientSecret = ""
}
}
workspaceSetting.IdentityProviders = append(workspaceSetting.IdentityProviders, identityProviderV1pb)
}
}
}

View File

@ -1,24 +1,68 @@
package license
import (
v1pb "github.com/yourselfhosted/slash/proto/gen/api/v1"
)
type FeatureType string
const (
// Enterprise features.
// FeatureTypeSSO allows the user to use SSO.
FeatureTypeSSO FeatureType = "ysh.slash.sso"
// FeatureTypeAdvancedAnalytics allows the user to use advanced analytics.
FeatureTypeAdvancedAnalytics FeatureType = "ysh.slash.advanced-analytics"
// Usages.
// FeatureTypeUnlimitedAccounts allows the user to create unlimited accounts.
FeatureTypeUnlimitedAccounts FeatureType = "unlimited_accounts"
FeatureTypeUnlimitedAccounts FeatureType = "ysh.slash.unlimited-accounts"
// FeatureTypeUnlimitedAccounts allows the user to create unlimited collections.
FeatureTypeUnlimitedCollections FeatureType = "unlimited_collections"
FeatureTypeUnlimitedCollections FeatureType = "ysh.slash.unlimited-collections"
// Customization.
// FeatureTypeCustomStyle allows the user to customize the style.
FeatureTypeCustomeStyle FeatureType = "custom_style"
// FeatureTypeCustomeBranding allows the user to customize the branding.
FeatureTypeCustomeBranding FeatureType = "ysh.slash.custom-branding"
)
// FeatureMatrix is a matrix of features in [Free, Pro].
var FeatureMatrix = map[FeatureType][2]bool{
FeatureTypeUnlimitedAccounts: {false, true},
FeatureTypeUnlimitedCollections: {false, true},
FeatureTypeCustomeStyle: {false, true},
func (f FeatureType) String() string {
return string(f)
}
// FeatureMatrix is a matrix of features in [Free, Pro, Enterprise].
var FeatureMatrix = map[FeatureType][3]bool{
FeatureTypeSSO: {false, false, false},
FeatureTypeAdvancedAnalytics: {false, false, false},
FeatureTypeUnlimitedAccounts: {false, true, false},
FeatureTypeUnlimitedCollections: {false, true, true},
FeatureTypeCustomeBranding: {false, true, true},
}
func getDefaultFeatures(plan v1pb.PlanType) []FeatureType {
var features []FeatureType
for feature, enabled := range FeatureMatrix {
if enabled[plan-1] {
features = append(features, feature)
}
}
return features
}
func validateFeatureString(feature string) (FeatureType, bool) {
switch feature {
case "ysh.slash.sso":
return FeatureTypeSSO, true
case "ysh.slash.advanced-analytics":
return FeatureTypeAdvancedAnalytics, true
case "ysh.slash.unlimited-accounts":
return FeatureTypeUnlimitedAccounts, true
case "ysh.slash.unlimited-collections":
return FeatureTypeUnlimitedCollections, true
case "ysh.slash.custom-branding":
return FeatureTypeCustomeBranding, true
default:
return "", false
}
}

View File

@ -3,6 +3,7 @@ package license
import (
"context"
_ "embed"
"slices"
"time"
"github.com/golang-jwt/jwt/v5"
@ -64,6 +65,10 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) (*v1pb.Subscripti
}
subscription.Plan = result.Plan
subscription.ExpiresTime = timestamppb.New(result.ExpiresTime)
subscription.Seats = int32(result.Seats)
for _, feature := range result.Features {
subscription.Features = append(subscription.Features, feature.String())
}
s.cachedSubscription = subscription
return subscription, nil
}
@ -104,28 +109,20 @@ func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey stri
return s.LoadSubscription(ctx)
}
func (s *LicenseService) GetSubscription(ctx context.Context) (*v1pb.Subscription, error) {
subscription, err := s.LoadSubscription(ctx)
if err != nil || subscription.Plan == v1pb.PlanType_PLAN_TYPE_UNSPECIFIED {
// nolint
return &v1pb.Subscription{
Plan: v1pb.PlanType_FREE,
}, nil
}
return subscription, nil
func (s *LicenseService) GetSubscription() *v1pb.Subscription {
return s.cachedSubscription
}
func (s *LicenseService) IsFeatureEnabled(feature FeatureType) bool {
matrix, ok := FeatureMatrix[feature]
if !ok {
return false
}
return matrix[s.cachedSubscription.Plan-1]
return slices.Contains(s.cachedSubscription.Features, feature.String())
}
type ValidateResult struct {
Plan v1pb.PlanType
ExpiresTime time.Time
Trial bool
Seats int
Features []FeatureType
}
type Claims struct {
@ -134,20 +131,34 @@ type Claims struct {
Owner string `json:"owner"`
Plan string `json:"plan"`
Trial bool `json:"trial"`
// The number of seats in the license key. Leave it empty if the license key does not have a seat limit.
Seats int `json:"seats"`
// The available features in the license key.
Features []string `json:"features"`
}
func validateLicenseKey(licenseKey string) (*ValidateResult, error) {
// Try to parse the license key as a JWT token.
claims, _ := parseLicenseKey(licenseKey)
if claims != nil {
result := &ValidateResult{
Plan: v1pb.PlanType(v1pb.PlanType_value[claims.Plan]),
ExpiresTime: claims.ExpiresAt.Time,
Trial: claims.Trial,
Seats: claims.Seats,
}
result.Features = getDefaultFeatures(result.Plan)
for _, feature := range claims.Features {
featureType, ok := validateFeatureString(feature)
if ok {
result.Features = append(result.Features, featureType)
}
}
plan := v1pb.PlanType(v1pb.PlanType_value[claims.Plan])
if plan == v1pb.PlanType_PLAN_TYPE_UNSPECIFIED {
return nil, errors.New("invalid plan")
}
return &ValidateResult{
Plan: v1pb.PlanType(v1pb.PlanType_value[claims.Plan]),
ExpiresTime: claims.ExpiresAt.Time,
}, nil
return result, nil
}
// Try to validate the license key with the license server.
@ -158,6 +169,11 @@ func validateLicenseKey(licenseKey string) (*ValidateResult, error) {
if validateResponse.Valid {
result := &ValidateResult{
Plan: v1pb.PlanType_PRO,
Features: []FeatureType{
FeatureTypeUnlimitedAccounts,
FeatureTypeUnlimitedCollections,
FeatureTypeCustomeBranding,
},
}
if validateResponse.LicenseKey.ExpiresAt != nil && *validateResponse.LicenseKey.ExpiresAt != "" {
expiresTime, err := time.Parse(time.RFC3339Nano, *validateResponse.LicenseKey.ExpiresAt)