--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Policies;
+
+use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
+use Cubist\Backpack\app\Magic\Models\CubistMagicAuthenticatable;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class CubistMagicPolicy
+{
+ use HandlesAuthorization;
+
+ public $permissionBase = '';
+ public $allowAll = false;
+
+ /**
+ * Determine whether the user can view any quizzes.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @return mixed
+ */
+ public function viewAny(CubistMagicAuthenticatable $user)
+ {
+ return $this->allowAll || $user->hasPermissionTo($this->permissionBase . ':admin');
+ }
+
+ public function viewList(CubistMagicAuthenticatable $user)
+ {
+ return $this->allowAll || $this->viewAny($user) || $user->hasPermissionTo($this->permissionBase . ':list');
+ }
+
+ /**
+ * Determine whether the user can view the quiz.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @param CubistMagicAbstractModel $model
+ * @return mixed
+ */
+ public function view(CubistMagicAuthenticatable $user, CubistMagicAbstractModel $model)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ if ($this->viewAny($user)) {
+ return true;
+ }
+ return $model->canView($user);
+ }
+
+ /**
+ * Determine whether the user can create quizzes.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @return mixed
+ */
+ public function create(CubistMagicAuthenticatable $user)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ if ($this->viewAny($user)) {
+ return true;
+ }
+ $user->hasPermissionTo($this->permissionBase . ':create');
+ }
+
+ /**
+ * Determine whether the user can update the quiz.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @param CubistMagicAbstractModel $model
+ * @return mixed
+ */
+ public function update(CubistMagicAuthenticatable $user, CubistMagicAbstractModel $model)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ return $model->canUpdate($user);
+ }
+
+ /**
+ * Determine whether the user can delete the quiz.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @param CubistMagicAbstractModel $model
+ * @return mixed
+ */
+ public function delete(CubistMagicAuthenticatable $user, CubistMagicAbstractModel $model)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ return $model->canDelete($user);
+ }
+
+ /**
+ * Determine whether the user can restore the quiz.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @param CubistMagicAbstractModel $model
+ * @return mixed
+ */
+ public function restore(CubistMagicAuthenticatable $user, CubistMagicAbstractModel $model)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ return $model->canRestore($user);
+ }
+
+ /**
+ * Determine whether the user can permanently delete the quiz.
+ *
+ * @param CubistMagicAuthenticatable $user
+ * @param CubistMagicAbstractModel $model
+ * @return mixed
+ */
+ public function forceDelete(CubistMagicAuthenticatable $user, CubistMagicAbstractModel $model)
+ {
+ if ($this->allowAll) {
+ return true;
+ }
+ return $model->canForceDelete($user);
+ }
+}