SocialLinkBar.php 2.05 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
<?php
/**
3
4
5
 * Copyright 2020 Art-ER S. Cons. P.A.
 * EROI - Emilia Romagna Open Innovation is based on:
 * https://www.open2.0.regione.lombardia.it
Marcello Pivanti's avatar
Marcello Pivanti committed
6
 *
7
 * @see https://repo.art-er.it Developers' community
8
9
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
 *
11
 * @package    arter\amos\socialauth\components
Marcello Pivanti's avatar
Marcello Pivanti committed
12
13
14
15
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
16
namespace arter\amos\socialauth\components;
Marcello Pivanti's avatar
Marcello Pivanti committed
17

waltermazza's avatar
waltermazza committed
18
19
20
use arter\amos\core\icons\AmosIcons;
use arter\amos\socialauth\models\SocialAuthUsers;
use arter\amos\socialauth\Module;
Marcello Pivanti's avatar
Marcello Pivanti committed
21
22
23
24
25
26
27
28
use Yii;
use yii\base\Component;
use yii\base\Widget;
use yii\db\ActiveRecord;
use yii\helpers\Html;

/**
 * Class FileImport
waltermazza's avatar
waltermazza committed
29
 * @package arter\amos\socialauth\components
Marcello Pivanti's avatar
Marcello Pivanti committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 */
class SocialLinkBar extends Widget
{
    public function run()
    {
        parent::run();

        /**
         * Return string
         */
        $result = '';

        /**
         * @var $module Module
         */
        $module = Yii::$app->getModule('socialauth');

        /**
         * List of providers configured
         */
        $providers = $module->providers;

        /**
         * @var $enabledProviders array List of providers not yet linked
         */
        $enabledProviders = [];

        /**
         * Iterate all provider and find existing links
         */
        foreach ($providers as $providerName=>$config) {
            $lowCaseName = strtolower($providerName);

            /**
             * @var $socialAccount SocialAuthUsers
             */
            $socialAccount = SocialAuthUsers::findOne([
                'provider' => $lowCaseName,
                'user_id' => Yii::$app->user->id
            ]);

            /**
             * If the user profile is not linked to this user append the provider
             */
            if(!$socialAccount || !$socialAccount->id) {
                $enabledProviders[$providerName] = $config;
            }
        }

        return $this->render('social-link-bar', [
            'providers' => $enabledProviders
        ]);
    }
}