Skip to content

Beautiful Open-Source Form Builder

Refactor Admin Panel with more features (#384)

Chirag Chhatrala053a4a4976b8
diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php
new file mode 100644
index 000000000..67eac5e96
--- /dev/null
+++ b/app/Http/Controllers/Admin/AdminController.php
@@ -0,0 +1,155 @@
+<?php
+
+namespace App\Http\Controllers\Admin;
+
+use App\Http\Controllers\Controller;
+use App\Models\Forms\Form;
+use App\Models\User;
+use Illuminate\Http\Request;
+use Laravel\Cashier\Cashier;
+
+class AdminController extends Controller
+{
+ public const ADMIN_LOG_PREFIX = '[admin_action] ';
+
+ public function __construct()
+ {
+ $this->middleware('moderator');
+ }
+
+ public function fetchUser($identifier)
+ {
+ $user = null;
+ if (is_numeric($identifier)) {
+ $user = User::find($identifier);
+ } elseif (filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
+ $user = User::whereEmail($identifier)->first();
+ } else {
+ // Find by form slug
+ $form = Form::whereSlug($identifier)->first();
+ if ($form) {
+ $user = $form->creator;
+ }
+ }
+
+ if (!$user) {
+ return $this->error([
+ 'message' => 'User not found.'
+ ]);
+ } elseif ($user->admin) {
+ return $this->error([
+ 'message' => 'You cannot fetch an admin.'
+ ]);
+ }
+
+ return $this->success([
+ 'user' => $user
+ ]);
+ }
+
+ public function applyDiscount(Request $request)
+ {
+ $request->validate([
+ 'user_id' => 'required'
+ ]);
+ $user = User::find($request->get("user_id"));
+
+ $activeSubscriptions = $user->subscriptions()->where(function ($q) {
+ $q->where('stripe_status', 'trialing')
+ ->orWhere('stripe_status', 'active');
+ })->get();
+
+ if ($activeSubscriptions->count() != 1) {
+ return $this->error([
+ "message" => "The user has more than one active subscriptions or doesn't have one."
+ ]);
+ }
+
+ $couponId = config('pricing.discount_coupon_id');
+ if (is_null($couponId)) {
+ return $this->error([
+ "message" => "Coupon id not defined."
+ ]);
+ }
+
+ $subscription = $activeSubscriptions->first();
+ Cashier::stripe()->subscriptions->update($subscription->stripe_id, [
+ 'coupon' => $couponId
+ ]);
+
+ \Log::warning(self::ADMIN_LOG_PREFIX . 'Applying NGO/Student discount to sub', [
+ 'user_id' => $user->id,
+ 'subcription_id' => $subscription->id,
+ 'coupon_id' => $couponId,
+ 'subscription_stripe_id' => $subscription->stripe_id,
+ 'moderator_id' => auth()->id(),
+ ]);
+
+ return $this->success([
+ "message" => "40% Discount applied for the next 12 months."
+ ]);
+ }
+
+ public function extendTrial(Request $request)
+ {
+ $request->validate([
+ 'user_id' => 'required',
+ 'number_of_day' => 'required|numeric|max:14'
+ ]);
+ $user = User::find($request->get("user_id"));
+
+ $subscription = $user->subscriptions()
+ ->where('stripe_status', 'trialing')
+ ->firstOrFail();
+
+ $trialEndDate = now()->addDays($request->get('number_of_day'));
+ $subscription->extendTrial($trialEndDate);
+
+ \Log::warning(self::ADMIN_LOG_PREFIX . 'Trial extended', [
+ 'user_id' => $user->id,
+ 'subcription_id' => $subscription->id,
+ 'nb_days' => $request->get('number_of_day'),
+ 'subscription_stripe_id' => $subscription->stripe_id,
+ 'moderator_id' => auth()->id(),
+ ]);
+
+ return $this->success([
+ "message" => "Subscription trial extend until the " . $trialEndDate->format('d/m/Y')
+ ]);
+ }
+
+ public function cancelSubscription(Request $request)
+ {
+ $request->validate([
+ 'user_id' => 'required',
+ 'cancellation_reason' => 'required'
+ ]);
+ $user = User::find($request->get("user_id"));
+
+ $activeSubscriptions = $user->subscriptions()->where(function ($q) {
+ $q->where('stripe_status', 'trialing')
+ ->orWhere('stripe_status', 'active');
+ })->get();
+
+ if ($activeSubscriptions->count() != 1) {
+ return $this->error([
+ "message" => "The user has more than one active subscriptions or doesn't have one."
+ ]);
+ }
+
+ $subscription = $activeSubscriptions->first();
+ $subscription->cancel();
+
+ \Log::warning(self::ADMIN_LOG_PREFIX . 'Cancel Subscription', [
+ 'user_id' => $user->id,
+ 'cancel_reason' => $request->get('cancellation_reason'),
+ 'moderator_id' => auth()->id(),
+ 'subcription_id' => $subscription->id,
+ 'subscription_stripe_id' => $subscription->stripe_id
+ ]);
+
+ return $this->success([
+ "message" => "The subscription cancellation has been successfully completed."
+ ]);
+ }
+}
diff --git a/app/Http/Controllers/Admin/ImpersonationController.php b/app/Http/Controllers/Admin/ImpersonationController.php
index 356ba2926..8155ecdc1 100644
--- a/app/Http/Controllers/Admin/ImpersonationController.php
+++ b/app/Http/Controllers/Admin/ImpersonationController.php
@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
-use App\Models\Forms\Form;
use App\Models\User;
class ImpersonationController extends Controller
@@ -13,22 +12,10 @@ class ImpersonationController extends Controller
$this->middleware('moderator');
}
- public function impersonate($identifier)
+ public function impersonate($userId)
{
- $user = null;
- if (is_numeric($identifier)) {
- $user = User::find($identifier);
- } elseif (filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
- $user = User::whereEmail($identifier)->first();
- } else {
- // Find by form slug
- $form = Form::whereSlug($identifier)->first();
- if ($form) {
- $user = $form->creator;
- }
- }
-
- if (! $user) {
+ $user = User::find($userId);
+ if (!$user) {
return $this->error([
'message' => 'User not found.',
]);
@@ -38,7 +25,7 @@ class ImpersonationController extends Controller
]);
}
- \Log::warning('Impersonation started', [
+ \Log::warning(AdminController::ADMIN_LOG_PREFIX . 'Impersonation started', [
'from_id' => auth()->id(),
'from_email' => auth()->user()->email,
'target_id' => $user->id,
diff --git a/client/components/global/VButton.vue b/client/components/global/VButton.vue
index b3edf7a19..ea56dca70 100644
--- a/client/components/global/VButton.vue
+++ b/client/components/global/VButton.vue
@@ -146,6 +146,14 @@ export default {
"ring-offset": "focus:ring-offset-gray-200",
text: "text-gray-500 hover:text-white",
}
+ } else if (this.color === "outline-red") {
+ return {
+ main: "bg-white border border-gray-300 text-red-700",
+ hover: "hover:bg-gray-100 hover:text-red-500",
+ ring: "focus:ring-gray-500",
+ "ring-offset": "focus:ring-offset-gray-200",
+ text: "text-gray-500",
+ }
} else if (this.color === "red") {
return {
main: "bg-red-600",
diff --git a/client/components/pages/admin/AdminCard.vue b/client/components/pages/admin/AdminCard.vue
new file mode 100644
index 000000000..2fda4615d
--- /dev/null
+++ b/client/components/pages/admin/AdminCard.vue
@@ -0,0 +1,23 @@
+<template>
+ <div class="w-full bg-white border border-gray-200 rounded-lg shadow flex flex-col">
+ <div class="w-full flex border-b px-4 py-2">
+ <Icon
+ :name="props.icon"
+ class="w-6 h-6 text-nt-blue"
+ />
+ <h3 class="text-md font-semibold ml-2">
+ {{ props.title }}
+ </h3>
+ </div>
+ <div class="p-4 flex-grow">
+ <slot />
+ </div>
+ </div>
+</template>
+
+<script setup>
+const props = defineProps({
+ title: { type: String, required: true },
+ icon: { type: String, required: true }
+})
+</script>
diff --git a/client/components/pages/admin/CancelSubscription.vue b/client/components/pages/admin/CancelSubscription.vue
new file mode 100644
index 000000000..d708d469a
--- /dev/null
+++ b/client/components/pages/admin/CancelSubscription.vue
@@ -0,0 +1,71 @@
+<template>
+ <AdminCard
+ title="Cancel subscription"
+ icon="heroicons:trash-16-solid"
+ >
+ <form
+ class="space-y-6 flex flex-col h-full justify-between"
+ @submit.prevent="askCancel"
+ >
+ <p class="text-xs text-gray-500">
+ Ideally customers should cancel subscription themselves via the UI. If
+ you cancel the subscription for them, please provide a reason.
+ </p>
+ <div>
+ <text-input
+ name="cancellation_reason"
+ :form="form"
+ label="Cancellation reason"
+ native-type="reason"
+ :required="true"
+ help="Cancellation reason"
+ />
+
+ <v-button
+ :loading="loading"
+ type="success"
+ class="w-full"
+ color="outline-red"
+ >
+ <Icon
+ class="inline w-4 h-4 text-red-600"
+ name="heroicons:exclamation-triangle-16-solid"
+ />
+ Cancel subscription now
+ </v-button>
+ </div>
+ </form>
+ </AdminCard>
+</template>
+
+<script setup>
+const props = defineProps({
+ user: { type: Object, required: true }
+})
+
+const alert = useAlert()
+let loading = ref(false)
+const form = useForm({
+ user_id: props.user.id,
+ cancellation_reason: ''
+})
+
+const askCancel = () => {
+ alert.confirm('Are you sure? This will cancel the subscription for this user.', cancelSubscription)
+}
+
+const cancelSubscription = () => {
+ loading = true
+ form
+ .patch('/moderator/cancellation-subscription')
+ .then(async (data) => {
+ loading = false
+ alert.success(data.message)
+ })
+ .catch((error) => {
+ alert.error(error.data.message)
+ loading = false
+ })
+}
+
+</script>
diff --git a/client/components/pages/admin/DiscountOnSubscription.vue b/client/components/pages/admin/DiscountOnSubscription.vue
new file mode 100644
index 000000000..0299bb31d
--- /dev/null
+++ b/client/components/pages/admin/DiscountOnSubscription.vue
@@ -0,0 +1,52 @@
+<template>
+ <AdminCard
+ title="Apply discount"
+ icon="heroicons:tag-20-solid"
+ >
+ <form
+ class="space-y-6 flex flex-col justify-between"
+ @submit.prevent="applyDiscount"
+ >
+ <p class="text-xs text-gray-500">
+ This is only for students, academics and NGOs. Make sure to verify
+ their status before applying discount (student/university email, NGO
+ website, proof of non-profit, etc). They need to create their
+ subscriptions before you can apply the 40% discount.
+ </p>
+ <v-button
+ :loading="loading"
+ type="success"
+ class="w-full"
+ color="white"
+ >
+ Apply Discount
+ </v-button>
+ </form>
+ </AdminCard>
+</template>
+
+<script setup>
+const props = defineProps({
+ user: { type: Object, required: true }
+})
+
+let loading = ref(false)
+const form = useForm({
+ user_id: props.user.id
+})
+
+const applyDiscount = () => {
+ loading = true
+ form
+ .patch('/moderator/apply-discount')
+ .then(async (data) => {
+ loading = false
+ useAlert().success(data.message)
+ })
+ .catch((error) => {
+ useAlert().error(error.data.message)
+ loading = false
+ })
+}
+
+</script>
diff --git a/client/components/pages/admin/ExtendTrial.vue b/client/components/pages/admin/ExtendTrial.vue
new file mode 100644
index 000000000..f86c2ea27
--- /dev/null
+++ b/client/components/pages/admin/ExtendTrial.vue
@@ -0,0 +1,63 @@
+<template>
+ <AdminCard
+ title="Extend trial"
+ icon="heroicons:calendar-16-solid"
+ >
+ <form
+ class="space-y-6 flex flex-col justify-between"
+ @submit.prevent="extendTrial"
+ >
+ <p class="text-xs text-gray-500">
+ You can extend the trial of subscribers that are still in the trial
+ period. Usually, you should not offer more than 7 days of trial, but
+ you can add up to 14 days if needed.
+ </p>
+ <div>
+ <text-input
+ name="number_of_day"
+ :form="form"
+ label="Number of days"
+ native-type="day"
+ :required="true"
+ help="Number Of Days"
+ placeholder="7"
+ />
+ <v-button
+ :loading="loading"
+ type="success"
+ class="w-full"
+ color="white"
+ >
+ Apply Extend Trial
+ </v-button>
+ </div>
+ </form>
+ </AdminCard>
+</template>
+
+<script setup>
+const props = defineProps({
+ user: { type: Object, required: true }
+})
+
+let loading = ref(false)
+const form = useForm({
+ user_id: props.user.id,
+ number_of_day: ''
+})
+
+const extendTrial = () => {
+ loading = true
+ form
+ .patch('/moderator/extend-trial')
+ .then(async (data) => {
+ loading = false
+ useAlert().success(data.message)
+ })
+ .catch((error) => {
+ useAlert().error(error.data.message)
+ loading = false
+ })
+}
+
+</script>
diff --git a/client/components/pages/admin/ImpersonateUser.vue b/client/components/pages/admin/ImpersonateUser.vue
new file mode 100644
index 000000000..f1c6d1582
--- /dev/null
+++ b/client/components/pages/admin/ImpersonateUser.vue
@@ -0,0 +1,53 @@
+<template>
+ <UButton
+ size="sm"
+ color="white"
+ icon="i-heroicons-eye-16-solid"
+ @click="impersonate"
+ >
+ Impersonate User
+ </UButton>
+</template>
+
+<script setup>
+const props = defineProps({
+ user: { type: Object, required: true }
+})
+
+const authStore = useAuthStore()
+const formsStore = useFormsStore()
+const workspacesStore = useWorkspacesStore()
+
+let loading = ref(false)
+
+const impersonate = () => {
+ loading = true
+ authStore.startImpersonating()
+ opnFetch(`/moderator/impersonate/${props.user.id}`).then(async (data) => {
+ loading = false
+
+ // Save the token.
+ authStore.setToken(data.token, false)
+
+ // Fetch the user.
+ const userData = await opnFetch('user')
+ authStore.setUser(userData)
+
+ // Redirect to the dashboard.
+ formsStore.set([])
+ workspacesStore.set([])
+
+ const workspaces = await fetchAllWorkspaces()
+ workspacesStore.set(workspaces.data.value)
+ formsStore.startLoading()
+ formsStore.loadAll(workspacesStore.currentId)
+
+ useAlert().success(`Impersonating ${authStore.user.name}`)
+ useRouter().push({ name: 'home' })
+ })
+ .catch((error) => {
+ useAlert().error(error.data.message)
+ loading = false
+ })
+}
+</script>
diff --git a/client/pages/settings/admin.vue b/client/pages/settings/admin.vue
index 4e5841888..eafe37e71 100644
--- a/client/pages/settings/admin.vue
+++ b/client/pages/settings/admin.vue
@@ -1,108 +1,154 @@
<template>
<div>
- <h3 class="font-semibold text-2xl text-gray-900">
- Admin settings
- </h3>
- <small class="text-gray-600">Manage settings.</small>
-
- <h3 class="mt-3 text-lg font-semibold mb-4">
- Tools
- </h3>
- <div class="flex flex-wrap mb-5">
- <a
- :href="statsUrl"
- target="_blank"
- >
- <v-button
- class="mx-1"
- color="gray"
- shade="lighter"
- > Stats </v-button>
- </a>
+ <div
+ v-if="userInfo"
+ class="flex gap-2 items-center"
+ >
+ <h1 class="text-xl">
+ {{ userInfo.name }}
+ </h1>
+ <div class="text-xs select-all bg-gray-50 rounded-md px-2 py-1 border">
+ {{ userInfo.id }}
+ </div>
+ <div class="text-xs select-all bg-gray-50 rounded-md px-2 py-1 border">
+ {{ userInfo.email }}
+ </div>
<a
- :href="horizonUrl"
+ v-if="userInfo.stripe_id"
+ :href="'https://dashboard.stripe.com/customers/'+userInfo.stripe_id"
target="_blank"
+ class="text-xs select-all bg-purple-50 border-purple-200 text-purple-500 rounded-md px-2 py-1 border"
>
- <v-button
- class="mx-1"
- color="gray"
- shade="lighter"
- > Horizon </v-button>
+ <Icon
+ name="bx:bxl-stripe"
+ class="h-4 w-4 inline-block"
+ />
+ {{ userInfo.stripe_id }}
</a>
</div>
- <h3 class="text-lg font-semibold mb-4">
- Impersonate User
- </h3>
- <form
- @submit.prevent="impersonate"
- @keydown="form.onKeydown($event)"
+ <h3
+ v-else
+ class="font-semibold text-2xl text-gray-900 mb-4"
>
- <!-- Password -->
- <text-input
- name="identifier"
- :form="form"
- label="Identifier"
- :required="true"
- help="User Id, User Email or Form Slug"
- />
+ Admin settings
+ </h3>
+
+
+ <template v-if="!userInfo">
+ <form
+ class="pb-8 max-w-lg"
+ @submit.prevent="fetchUser"
+ @keydown="fetchUserForm.onKeydown($event)"
+ >
+ <text-input
+ name="identifier"
+ :form="fetchUserForm"
+ label="Identifier"
+ :required="true"
+ help="User Id, User Email, Form Slug or View Slug"
+ />
+ <v-button
+ :loading="loading"
+ type="success"
+ color="blue"
+ class="mt-4 w-full"
+ >
+ Fetch User
+ </v-button>
+ </form>
+ </template>
- <!-- Submit Button -->
- <v-button
- :loading="loading"
- class="mt-4"
+ <div
+ v-else
+ class="flex flex-col"
+ >
+ <div
+ id="admin-buttons"
+ class="flex gap-1 my-4"
>
- Impersonate User
- </v-button>
- </form>
+ <impersonate-user :user="userInfo" />
+ </div>
+ <div
+ class="w-full grid gap-2 grid-cols-1 lg:grid-cols-2"
+ >
+ <discount-on-subscription
+ :user="userInfo"
+ />
+ <extend-trial
+ :user="userInfo"
+ />
+ <cancel-subscription
+ :user="userInfo"
+ />
+ </div>
+ </div>
</div>
</template>
-<script setup>
-import { useRouter } from "vue-router"
-import { opnFetch } from "~/composables/useOpnApi.js"
-import { fetchAllWorkspaces } from "~/stores/workspaces.js"
+<script>
+import { computed } from 'vue'
-definePageMeta({
- middleware: "moderator",
-})
+export default {
+ setup () {
+ useOpnSeoMeta({
+ title: 'Admin'
+ })
+ definePageMeta({
+ middleware: 'moderator'
+ })
-useOpnSeoMeta({
- title: "Admin",
-})
+ const authStore = useAuthStore()
+ return {
+ authStore,
+ user: computed(() => authStore.user),
+ useAlert: useAlert()
+ }
+ },
-const authStore = useAuthStore()
-const workspacesStore = useWorkspacesStore()
-const router = useRouter()
-const form = useForm({
- identifier: "",
-})
-const loading = ref(false)
+ data: () => ({
+ userInfo: null,
+ fetchUserForm: useForm({
+ identifier: ''
+ }),
+ loading: false
+ }),
-const runtimeConfig = useRuntimeConfig()
-const statsUrl = runtimeConfig.public.apiBase + "/stats"
-const horizonUrl = runtimeConfig.public.apiBase + "/horizon"
+ computed: {
+ isAdmin () {
+ return this.user.admin
+ }
+ },
-const impersonate = () => {
- loading.value = true
- authStore.startImpersonating()
- opnFetch("/admin/impersonate/" + encodeURI(form.identifier))
- .then(async (data) => {
- // Save the token.
- authStore.setToken(data.token, false)
+ mounted () {
+ // Shortcut link to impersonate users
+ const urlSearchParams = new URLSearchParams(window.location.search)
+ const params = Object.fromEntries(urlSearchParams.entries())
+ if (params.impersonate) {
+ this.fetchUserForm.identifier = params.impersonate
+ }
+ if (params.user_id) {
+ this.fetchUserForm.identifier = params.user_id
+ }
+ },
- // Fetch the user.
- const userData = await opnFetch("user")
- authStore.setUser(userData)
- const workspaces = await fetchAllWorkspaces()
- workspacesStore.set(workspaces.data.value)
- loading.value = false
+ methods: {
+ async fetchUser () {
+ if (!this.fetchUserForm.identifier) {
+ this.useAlert.error('Identifier is required.')
+ return
+ }
- router.push({ name: "home" })
- })
- .catch((error) => {
- console.error(error)
- useAlert().error(error.data.message)
- loading.value = false
- })
+ this.loading = true
+ opnFetch(`/moderator/fetch-user/${encodeURI(this.fetchUserForm.identifier)}`).then(async (data) => {
+ this.loading = false
+ this.userInfo = data.user
+ this.useAlert.success(`User Fetched: ${this.userInfo.name}`)
+ })
+ .catch((error) => {
+ this.useAlert.error(error.data.message)
+ this.loading = false
+ })
+ }
+ }
}
-</script>
+</script>
\ No newline at end of file
diff --git a/client/stores/auth.js b/client/stores/auth.js
index 099db9cff..8e2e67cd5 100644
--- a/client/stores/auth.js
+++ b/client/stores/auth.js
@@ -21,7 +21,7 @@ export const useAuthStore = defineStore("auth", {
// Stop admin impersonation
stopImpersonating() {
this.setToken(this.admin_token)
- this.admin_token = null
+ this.setAdminToken(null)
},
setToken(token) {
diff --git a/config/pricing.php b/config/pricing.php
index 965ce1f38..5465bca13 100644
--- a/config/pricing.php
+++ b/config/pricing.php
@@ -22,4 +22,5 @@ return [
],
],
+ 'discount_coupon_id' => env('STRIPE_DISCOUNT_COUPON_ID', null),
];
diff --git a/routes/api.php b/routes/api.php
index b7f84cabb..8f55b10dd 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -160,11 +160,27 @@ Route::group(['middleware' => 'auth:api'], function () {
});
});
- Route::group(['middleware' => 'moderator', 'prefix' => 'admin'], function () {
+ Route::group(['middleware' => 'moderator', 'prefix' => 'moderator'], function () {
Route::get(
- 'impersonate/{identifier}',
+ 'fetch-user/{identifier}',
+ [\App\Http\Controllers\Admin\AdminController::class, 'fetchUser']
+ );
+ Route::get(
+ 'impersonate/{userId}',
[\App\Http\Controllers\Admin\ImpersonationController::class, 'impersonate']
);
+ Route::patch(
+ 'apply-discount',
+ [\App\Http\Controllers\Admin\AdminController::class, 'applyDiscount']
+ );
+ Route::patch(
+ 'extend-trial',
+ [\App\Http\Controllers\Admin\AdminController::class, 'extendTrial']
+ );
+ Route::patch(
+ 'cancellation-subscription',
+ [\App\Http\Controllers\Admin\AdminController::class, 'cancelSubscription']
+ );
});
});