Browse Source

Add "Delete Profile" to Profile\Edit page

Will not work until the update to revision 63 is completed.
Visman 2 years ago
parent
commit
c21fdc7423

+ 9 - 0
app/Controllers/Routing.php

@@ -269,6 +269,15 @@ class Routing
                     'EditUserPass'
                     'EditUserPass'
                 );
                 );
             }
             }
+            // удаление своего профиля
+            if (! $user->isGuest) {
+                $r->add(
+                    $r::DUO,
+                    '/user/{id|i:' . $user->id . '}/delete/profile',
+                    'ProfileDelete:delete',
+                    'DeleteUserProfile'
+                );
+            }
             // управление аккаунтами OAuth
             // управление аккаунтами OAuth
             if (
             if (
                 ! $user->isGuest
                 ! $user->isGuest

+ 18 - 0
app/Models/Pages/Admin/Update.php

@@ -762,4 +762,22 @@ class Update extends Admin
 
 
         return null;
         return null;
     }
     }
+
+    /**
+     * rev.62 to rev.63
+     */
+    protected function stageNumber62(array $args): ?int
+    {
+        $coreConfig = new CoreConfig($this->configFile);
+
+        $coreConfig->add(
+            'multiple=>ProfileDelete',
+            '\\ForkBB\\Models\\Pages\\Profile\\Delete::class',
+            'ProfileOAuth'
+        );
+
+        $coreConfig->save();
+
+        return null;
+    }
 }
 }

+ 122 - 0
app/Models/Pages/Profile/Delete.php

@@ -0,0 +1,122 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+namespace ForkBB\Models\Pages\Profile;
+
+use ForkBB\Core\Validator;
+use ForkBB\Models\Page;
+use ForkBB\Models\Pages\Profile;
+use function \ForkBB\__;
+
+class Delete extends Profile
+{
+    /**
+     * Подготавливает данные для шаблона удаления аккаунта
+     */
+    public function delete(array $args, string $method): Page
+    {
+        if (
+            false === $this->initProfile($args['id'])
+            || ! $this->rules->deleteMyProfile
+        ) {
+            return $this->c->Message->message('Bad request');
+        }
+
+        $this->c->Lang->load('validator');
+
+        if ('POST' === $method) {
+            $v = $this->c->Validator->reset()
+                ->addValidators([
+                    'check_password' => [$this, 'vCheckPassword'],
+                ])->addRules([
+                    'token'    => 'token:DeleteUserProfile',
+                    'password' => 'required|string:trim|max:100000|check_password',
+                    'confirm'  => 'required|integer|in:0,1',
+                    'delete'   => 'required|string',
+                ])->addAliases([
+                    'password' => 'Your passphrase',
+                ])->addArguments([
+                    'token'    => $args,
+                ])->addMessages([
+                ]);
+
+            $valid = $v->validation($_POST);
+
+            if (0 === $v->confirm) {
+                return $this->c->Redirect->page('EditUserProfile', $args)->message('No confirm redirect', FORK_MESS_VLD);
+            } elseif ($valid) {
+                $this->c->Cookie->deleteUser();
+                $this->c->users->delete($this->user);
+
+                return $this->c->Redirect->page('Index')->message('Your deleted redirect', FORK_MESS_SUCC);
+            }
+
+            $this->fIswev = $v->getErrors();
+        }
+
+        $this->fIswev          = [FORK_MESS_ERR, 'You are trying to delete your profile'];
+        $this->crumbs          = $this->crumbs(
+            [
+                $this->c->Router->link('DeleteUserProfile', $args),
+                'Deleting profile',
+            ],
+            [
+                $this->c->Router->link('EditUserProfile', $args),
+                'Editing profile',
+            ]
+        );
+        $this->form            = $this->form($args);
+        $this->actionBtns      = $this->btns('edit');
+        $this->profileIdSuffix = '-delete';
+
+        return $this;
+    }
+
+    /**
+     * Создает массив данных для формы
+     */
+    protected function form(array $args): array
+    {
+        $yn   = [1 => __('Yes'), 0 => __('No')];
+        $form = [
+            'action' => $this->c->Router->link('DeleteUserProfile', $args),
+            'hidden' => [
+                'token' => $this->c->Csrf->create('DeleteUserProfile', $args),
+            ],
+            'sets'   => [
+                'delete' => [
+                    'class'  => ['data-edit'],
+                    'fields' => [
+                        'confirm' => [
+                            'type'    => 'radio',
+                            'value'   => '0',
+                            'values'  => $yn,
+                            'caption' => 'Confirm delete label',
+                            'help'    => 'Confirm delete info',
+                        ],
+                        'password' => [
+                            'type'      => 'password',
+                            'caption'   => 'Your passphrase',
+                            'required'  => true,
+                        ],
+                    ],
+                ],
+            ],
+            'btns'   => [
+                'delete' => [
+                    'type'  => 'submit',
+                    'value' => __('Delete'),
+                ],
+            ],
+        ];
+
+        return $form;
+    }
+}

+ 20 - 0
app/Models/Pages/Profile/Edit.php

@@ -239,6 +239,7 @@ class Edit extends Profile
 
 
         // имя, титул и аватара
         // имя, титул и аватара
         $fields = [];
         $fields = [];
+
         if ($this->rules->rename) {
         if ($this->rules->rename) {
             $fields['username'] = [
             $fields['username'] = [
                 'type'      => 'text',
                 'type'      => 'text',
@@ -257,6 +258,16 @@ class Edit extends Profile
                 'value'   => $this->curUser->username,
                 'value'   => $this->curUser->username,
             ];
             ];
         }
         }
+
+        if ($this->rules->deleteMyProfile) {
+            $fields['delete_profile'] = [
+                'type'    => 'link',
+                'value'   => __('Delete my profile'),
+                'title'   => __('Delete my profile'),
+                'href'    => $this->c->Router->link('DeleteUserProfile', $args),
+            ];
+        }
+
         if ($this->rules->changeGroup) {
         if ($this->rules->changeGroup) {
             $fields['group'] = [
             $fields['group'] = [
                 'type'    => 'link',
                 'type'    => 'link',
@@ -273,6 +284,7 @@ class Edit extends Profile
                 'value'   => $this->curUser->group_id ? $this->curUser->g_title : '-',
                 'value'   => $this->curUser->group_id ? $this->curUser->g_title : '-',
             ];
             ];
         }
         }
+
         if ($this->rules->confModer) {
         if ($this->rules->confModer) {
             $fields['configure-moderator'] = [
             $fields['configure-moderator'] = [
                 'type'    => 'link',
                 'type'    => 'link',
@@ -280,6 +292,7 @@ class Edit extends Profile
                 'href'    => $this->c->Router->link('EditUserModeration', $args),
                 'href'    => $this->c->Router->link('EditUserModeration', $args),
             ];
             ];
         }
         }
+
         if ($this->rules->setTitle) {
         if ($this->rules->setTitle) {
             $fields['title'] = [
             $fields['title'] = [
                 'type'      => 'text',
                 'type'      => 'text',
@@ -296,6 +309,7 @@ class Edit extends Profile
                 'value'   => $this->curUser->title(),
                 'value'   => $this->curUser->title(),
             ];
             ];
         }
         }
+
         if ($this->rules->editPass) {
         if ($this->rules->editPass) {
             $fields['change_pass'] = [
             $fields['change_pass'] = [
                 'type'  => 'link',
                 'type'  => 'link',
@@ -303,6 +317,7 @@ class Edit extends Profile
                 'href'  => $this->c->Router->link('EditUserPass', $args),
                 'href'  => $this->c->Router->link('EditUserPass', $args),
             ];
             ];
         }
         }
+
         if ($this->rules->configureOAuth) {
         if ($this->rules->configureOAuth) {
             $fields['configure_oauth'] = [
             $fields['configure_oauth'] = [
                 'type'  => 'link',
                 'type'  => 'link',
@@ -310,6 +325,7 @@ class Edit extends Profile
                 'href'  => $this->c->Router->link('EditUserOAuth', $args),
                 'href'  => $this->c->Router->link('EditUserOAuth', $args),
             ];
             ];
         }
         }
+
         if ($this->rules->useAvatar) {
         if ($this->rules->useAvatar) {
             if (! $this->curUser->avatar) {
             if (! $this->curUser->avatar) {
                 $fields['avatar'] = [
                 $fields['avatar'] = [
@@ -405,6 +421,7 @@ class Edit extends Profile
 
 
         // контактная информация
         // контактная информация
         $fields = [];
         $fields = [];
+
         if ($this->rules->viewOEmail) {
         if ($this->rules->viewOEmail) {
             $fields['open-email'] = [
             $fields['open-email'] = [
                 'class'   => ['pline'],
                 'class'   => ['pline'],
@@ -413,6 +430,7 @@ class Edit extends Profile
                 'value'   => $this->curUser->censorEmail,
                 'value'   => $this->curUser->censorEmail,
             ];
             ];
         }
         }
+
         if ($this->rules->editEmail) {
         if ($this->rules->editEmail) {
             $fields['change_email'] = [
             $fields['change_email'] = [
                 'type'  => 'link',
                 'type'  => 'link',
@@ -420,6 +438,7 @@ class Edit extends Profile
                 'href'  => $this->c->Router->link('EditUserEmail', $args),
                 'href'  => $this->c->Router->link('EditUserEmail', $args),
             ];
             ];
         }
         }
+
         $fields['email_setting'] = [
         $fields['email_setting'] = [
             'class'   => ['block'],
             'class'   => ['block'],
             'type'    => 'radio',
             'type'    => 'radio',
@@ -453,6 +472,7 @@ class Edit extends Profile
                 'href'    => $this->curUser->censorUrl,
                 'href'    => $this->curUser->censorUrl,
             ];
             ];
         }
         }
+
         $form['sets']['contacts'] = [
         $form['sets']['contacts'] = [
             'class'  => ['data-edit'],
             'class'  => ['data-edit'],
             'legend' => 'Contact details',
             'legend' => 'Contact details',

+ 1 - 0
app/config/main.dist.php

@@ -374,6 +374,7 @@ return [
         'ProfileEmail'       => \ForkBB\Models\Pages\Profile\Email::class,
         'ProfileEmail'       => \ForkBB\Models\Pages\Profile\Email::class,
         'ProfileMod'         => \ForkBB\Models\Pages\Profile\Mod::class,
         'ProfileMod'         => \ForkBB\Models\Pages\Profile\Mod::class,
         'ProfileOAuth'       => \ForkBB\Models\Pages\Profile\OAuth::class,
         'ProfileOAuth'       => \ForkBB\Models\Pages\Profile\OAuth::class,
+        'ProfileDelete'      => \ForkBB\Models\Pages\Profile\Delete::class,
         'AdminIndex'         => \ForkBB\Models\Pages\Admin\Index::class,
         'AdminIndex'         => \ForkBB\Models\Pages\Admin\Index::class,
         'AdminStatistics'    => \ForkBB\Models\Pages\Admin\Statistics::class,
         'AdminStatistics'    => \ForkBB\Models\Pages\Admin\Statistics::class,
         'AdminOptions'       => \ForkBB\Models\Pages\Admin\Options::class,
         'AdminOptions'       => \ForkBB\Models\Pages\Admin\Options::class,

+ 18 - 0
app/lang/en/profile.po

@@ -335,3 +335,21 @@ msgstr "Add new account"
 
 
 msgid "Delete"
 msgid "Delete"
 msgstr "Delete"
 msgstr "Delete"
+
+msgid "Delete my profile"
+msgstr "Delete profile"
+
+msgid "Deleting profile"
+msgstr "Delete profile"
+
+msgid "You are trying to delete your profile"
+msgstr "You are trying to delete your profile."
+
+msgid "Confirm delete label"
+msgstr "Delete profile?"
+
+msgid "Confirm delete info"
+msgstr "<b>This is an irreversible operation!</b>"
+
+msgid "Your deleted redirect"
+msgstr "Your profile has been deleted."

+ 18 - 0
app/lang/ru/profile.po

@@ -335,3 +335,21 @@ msgstr "Привязать новый аккаунт"
 
 
 msgid "Delete"
 msgid "Delete"
 msgstr "Удалить"
 msgstr "Удалить"
+
+msgid "Delete my profile"
+msgstr "Удалить профиль"
+
+msgid "Deleting profile"
+msgstr "Удаление профиля"
+
+msgid "You are trying to delete your profile"
+msgstr "Вы пытаетесь удалить свой профиль."
+
+msgid "Confirm delete label"
+msgstr "Удалить профиль?"
+
+msgid "Confirm delete info"
+msgstr "<b>Это необратимая операция!</b>"
+
+msgid "Your deleted redirect"
+msgstr "Ваш профиль удален."