瀏覽代碼

OAuth (part 8 draft)

Visman 2 年之前
父節點
當前提交
29e11e915f

+ 131 - 4
app/Models/Pages/Profile/OAuth.php

@@ -39,7 +39,7 @@ class OAuth extends Profile
 
         $this->c->Lang->load('admin_providers');
 
-        $this->crumbs     = $this->crumbs(
+        $this->crumbs          = $this->crumbs(
             [
                 $this->c->Router->link('EditUserOAuth', $args),
                 'OAuth accounts',
@@ -49,9 +49,10 @@ class OAuth extends Profile
                 'Editing profile',
             ]
         );
-        $this->form       = $this->formList($args);
-        $this->formOAuth  = $this->reglogForm();
-        $this->actionBtns = $this->btns('edit');
+        $this->form            = $this->formList($args);
+        $this->formOAuth       = $this->reglogForm();
+        $this->actionBtns      = $this->btns('edit');
+        $this->profileIdSuffix = '-oauth';
 
         return $this;
     }
@@ -97,4 +98,130 @@ class OAuth extends Profile
             'btns'   => null,
         ];
     }
+
+    /**
+     * Подготавливает данные для шаблона списка аккаунтов
+     */
+    public function action(array $args, string $method): Page
+    {
+        if (
+            false === $this->initProfile($args['id'])
+            || ! $this->rules->configureOAuth
+        ) {
+            return $this->c->Message->message('Bad request');
+        }
+
+        list($name, $userId) = \array_pad(\explode('-', $args['key'], 2), 2, null);
+
+        $data   = $this->c->providerUser->loadUserData($this->curUser);
+        $puInfo = null;
+
+        foreach ($data as $cur) {
+            if (
+                $name === $cur['name']
+                && $userId === $cur['userId']
+            ) {
+                $puInfo = $cur;
+
+                break;
+            }
+        }
+
+        if (empty($puInfo)) {
+            return $this->c->Message->message('Bad request');
+        }
+
+        $this->c->Lang->load('admin_providers');
+        $this->c->Lang->load('validator');
+
+        if ('POST' === $method) {
+        }
+
+
+
+
+
+
+        $this->crumbs          = $this->crumbs(
+            [
+                $this->c->Router->link('EditUserOAuthAction', $args),
+                $name,
+            ],
+            [
+                $this->c->Router->link('EditUserOAuth', $args),
+                'OAuth accounts',
+            ],
+            [
+                $this->c->Router->link('EditUserProfile', $args),
+                'Editing profile',
+            ]
+        );
+        $this->form            = $this->formAction($puInfo, $args);
+        $this->actionBtns      = $this->btns('edit');
+        $this->profileIdSuffix = '-oauth-a';
+
+        return $this;
+    }
+
+    /**
+     * Создает массив данных для формы днействия
+     */
+    protected function formAction(array $info, array $args): array
+    {
+        return [
+            'action' => $this->c->Router->link('EditUserOAuthAction', $args),
+            'hidden' => [
+                'token' => $this->c->Csrf->create('EditUserOAuthAction', $args),
+            ],
+            'sets'   => [
+                'oauth-account' => [
+                    'class'  => ['data-edit'],
+                    'fields' => [
+                        'provider' => [
+                            'type'    => 'str',
+                            'class'   => ['pline'],
+                            'caption' => 'Provider label',
+                            'value'   => __($info['name']),
+                        ],
+                        'userId' => [
+                            'type'    => 'str',
+                            'class'   => ['pline'],
+                            'caption' => 'Identifier label',
+                            'value'   => $info['userId'],
+                        ],
+                        'userEmail' => [
+                            'type'    => 'str',
+                            'class'   => ['pline'],
+                            'caption' => 'Email label',
+                            'value'   => $info['userEmail'],
+                        ],
+                        'userEmailVerifed' => [
+                            'type'    => 'str',
+                            'class'   => ['pline'],
+                            'caption' => 'Verified label',
+                            'value'   => __($info['userEmailVerifed'] ? 'Yes' : 'No'),
+                        ],
+                        'confirm'  => [
+                            'type'    => 'checkbox',
+                            'class'   => ['pline'],
+                            'label'   => 'Confirm action',
+                            'checked' => false,
+                        ],
+                    ],
+                ],
+            ],
+            'btns'   => [
+                'delete' => [
+                    'type'  => 'submit',
+                    'value' => __('Delete'),
+                ],
+                'cancel' => [
+                    'type'  => 'btn',
+                    'value' => __('Cancel'),
+                    'link'  => $this->c->Router->link('EditUserOAuth', $args),
+                ],
+            ],
+        ];
+
+    }
 }

+ 43 - 7
app/Models/Pages/RegLog.php

@@ -10,8 +10,6 @@ declare(strict_types=1);
 
 namespace ForkBB\Models\Pages;
 
-use ForkBB\Core\Validator;
-use ForkBB\Core\Exceptions\MailException;
 use ForkBB\Models\Page;
 use ForkBB\Models\Provider\Driver;
 use ForkBB\Models\User\User;
@@ -81,14 +79,17 @@ class RegLog extends Page
             }
         }
 
+        $uid = $this->c->providerUser->findUser($provider);
+
         // гость
         if ($this->user->isGuest) {
-            $uid = $this->c->providerUser->findUser($provider);
-
             // регистрация
             if (empty($uid)) {
                 // на форуме есть пользователь с таким email
-                if ($this->c->providerUser->findEmail($provider) > 0) {
+                if (
+                    $this->c->providerUser->findEmail($provider) > 0
+                    || $this->c->users->loadByEmail($provider->userEmail) instanceof User
+                ) {
                     $auth         = $this->c->Auth;
                     $auth->fIswev = ['i', ['Email message', __($provider->name)]];
 
@@ -119,7 +120,7 @@ class RegLog extends Page
                 $user->location        = $provider->userLocation;
                 $user->url             = $provider->userURL;
 
-                $newUserId = $this->c->users->insert($user);
+                $this->c->users->insert($user);
 
                 if (true !== $this->c->providerUser->registration($user, $provider)) {
                     throw new RuntimeException('Failed to insert data'); // ??????????????????????????????????????????
@@ -141,7 +142,42 @@ class RegLog extends Page
 
         // пользователь
         } else {
-            return $this->c->Message->message('Bad request');
+            $redirect = $this->c->Redirect->page('EditUserOAuth', ['id' => $this->user->id]);
+
+            // аккаунт есть и он привязан к текущему пользователю
+            if ($uid === $this->user->id) {
+                return $redirect->message('Already linked to you', 5);
+
+            // аккаунт есть и он привязан к другому пользователю
+            } elseif ($uid > 0) {
+                return $redirect->message('Already linked to another', 5);
+            }
+
+            $uid = $this->c->providerUser->findEmail($provider);
+
+            // email принадлежит другому пользователю
+            if (
+                $uid
+                && $uid !== $this->user->id
+            ) {
+                return $redirect->message(['Email registered by another', __($provider->name)], 5);
+            }
+
+            $user = $this->c->users->loadByEmail($provider->userEmail);
+
+            // email принадлежит другому пользователю
+            if (
+                $user instanceof User
+                && $user !== $this->user
+            ) {
+                return $redirect->message(['Email registered by another', __($provider->name)], 5);
+            }
+
+            if (true !== $this->c->providerUser->registration($this->user, $provider)) {
+                throw new RuntimeException('Failed to insert data'); // ??????????????????????????????????????????
+            }
+
+            return $redirect->message('Account linked');
         }
     }
 

+ 21 - 0
app/lang/en/admin_providers.po

@@ -83,3 +83,24 @@ msgstr "This board is not accepting new registrations."
 
 msgid "Email message"
 msgstr "This email received from %s belongs to a registered user.<br>Perhaps you used another provider to sign in or username + passphrase?"
+
+msgid "Already linked to you"
+msgstr "This account is already linked to you."
+
+msgid "Already linked to another"
+msgstr "This account is already linked to another user."
+
+msgid "Email registered by another"
+msgstr "The email address received from %s is registered by another user."
+
+msgid "Account linked"
+msgstr "Account linked."
+
+msgid "Identifier label"
+msgstr "Identifier"
+
+msgid "Email label"
+msgstr "Email"
+
+msgid "Verified label"
+msgstr "Email verified"

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

@@ -332,3 +332,6 @@ msgstr "Linked accounts"
 
 msgid "Add account"
 msgstr "Add new account"
+
+msgid "Delete"
+msgstr "Delete"

+ 21 - 0
app/lang/ru/admin_providers.po

@@ -83,3 +83,24 @@ msgstr "Форум закрыт для новых регистраций."
 
 msgid "Email message"
 msgstr "Данный электронный адрес, полученный от %s, принадлежит зарегистрированному пользователю.<br>Возможно, вы использовали другого провайдера для входа или имя + кодовую фразу?"
+
+msgid "Already linked to you"
+msgstr "Данный аккаунт уже привязан к вам."
+
+msgid "Already linked to another"
+msgstr "Данный аккаунт уже привязан к другому пользователю."
+
+msgid "Email registered by another"
+msgstr "Электронный адрес полученный от %s зарегистрирован другим пользователем."
+
+msgid "Account linked"
+msgstr "Аккаунт привязан."
+
+msgid "Identifier label"
+msgstr "Идентификатор"
+
+msgid "Email label"
+msgstr "Электронный адрес"
+
+msgid "Verified label"
+msgstr "Адрес проверен"

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

@@ -332,3 +332,6 @@ msgstr "Связанные аккаунты"
 
 msgid "Add account"
 msgstr "Привязать новый аккаунт"
+
+msgid "Delete"
+msgstr "Удалить"

+ 3 - 2
public/style/ForkBB/style.css

@@ -2493,8 +2493,9 @@ body,
   flex-wrap: wrap;
 }
 
-#fork .f-field-oauth-acc-btn {
-  flex: 1 0 17rem;
+#fork .f-field-oauth-acc-btn,
+#fork-profile-oauth-a .f-fbtn {
+  flex: 1 0 7rem;
 }
 
 @media screen and (min-width: 40rem) {