Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krivochenko committed Jun 7, 2015
0 parents commit 1023263
Show file tree
Hide file tree
Showing 73 changed files with 4,485 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace budyaga\users;

use Yii;

class Module extends \yii\base\Module
{
public $controllerNamespace = 'budyaga\users\controllers';

public $customViews = [];

public $customMailViews = [];

public function init()
{
parent::init();

self::registerTranslations();
}

public static function registerTranslations()
{
if (!isset(Yii::$app->i18n->translations['users']) && !isset(Yii::$app->i18n->translations['users/*'])) {
Yii::$app->i18n->translations['users'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@budyaga/users/messages',
'forceTranslation' => true,
'fileMap' => [
'users' => 'users.php'
]
];
}
}

public function getCustomView($default)
{
if (isset($this->customViews[$default])) {
return $this->customViews[$default];
} else {
return $default;
}
}

public function getCustomMailView($default)
{
if (isset($this->customMailViews[$default])) {
return $this->customMailViews[$default];
} else {
return '@budyaga/users/mail/' . $default;
}
}
}
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Module мanagement users
=======================
Module for manage users and their rights with the support of registration through social services and assigned to each user more than one social service.

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist budyaga/yii2-users "*"
```

or add

```
"budyaga/yii2-users": "*"
```

to the require section of your `composer.json` file.


Usage
-----
30 changes: 30 additions & 0 deletions UsersAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace budyaga\users;

use yii\web\AssetBundle;

/**
* Widget asset bundle
*/
class UsersAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = '@budyaga/users/assets';

/**
* @inheritdoc
*/
public $css = [
'css/styles.css'
];

/**
* @inheritdoc
*/
public $depends = [
'yii\web\JqueryAsset'
];
}
7 changes: 7 additions & 0 deletions assets/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.auth-icon {
display: inline-block;
height: 32px;
padding-left: 40px;
line-height: 32px;
margin-bottom: 10px;
}
Binary file added assets/img/female.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/male.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
218 changes: 218 additions & 0 deletions components/AuthChoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

namespace budyaga\users\components;

use yii\base\InvalidConfigException;
use yii\base\Widget;
use Yii;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\authclient\ClientInterface;
use yii\authclient\widgets\AuthChoiceAsset;

class AuthChoice extends \yii\authclient\widgets\AuthChoice
{
/**
* @var string name of the auth client collection application component.
* This component will be used to fetch services value if it is not set.
*/
public $clientCollection = 'authClientCollection';

public $clientCssClass = 'col-xs-3';
/**
* @var string name of the GET param , which should be used to passed auth client id to URL
* defined by [[baseAuthUrl]].
*/
public $clientIdGetParamName = 'authclient';
/**
* @var array the HTML attributes that should be rendered in the div HTML tag representing the container element.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [
'class' => ''
];
/**
* @var boolean indicates if popup window should be used instead of direct links.
*/
public $popupMode = true;
/**
* @var boolean indicates if widget content, should be rendered automatically.
* Note: this value automatically set to 'false' at the first call of [[createClientUrl()]]
*/
public $autoRender = true;
/**
* @var array configuration for the external clients base authentication URL.
*/
private $_baseAuthUrl;
/**
* @var ClientInterface[] auth providers list.
*/
private $_clients;


/**
* @param ClientInterface[] $clients auth providers
*/
public function setClients(array $clients)
{
$this->_clients = $clients;
}

/**
* @return ClientInterface[] auth providers
*/
public function getClients()
{
if ($this->_clients === null) {
$this->_clients = $this->defaultClients();
}

return $this->_clients;
}

/**
* @param array $baseAuthUrl base auth URL configuration.
*/
public function setBaseAuthUrl(array $baseAuthUrl)
{
$this->_baseAuthUrl = $baseAuthUrl;
}

/**
* @return array base auth URL configuration.
*/
public function getBaseAuthUrl()
{
if (!is_array($this->_baseAuthUrl)) {
$this->_baseAuthUrl = $this->defaultBaseAuthUrl();
}

return $this->_baseAuthUrl;
}

/**
* Returns default auth clients list.
* @return ClientInterface[] auth clients list.
*/
protected function defaultClients()
{
/* @var $collection \yii\authclient\Collection */
$collection = Yii::$app->get($this->clientCollection);

return $collection->getClients();
}

/**
* Composes default base auth URL configuration.
* @return array base auth URL configuration.
*/
protected function defaultBaseAuthUrl()
{
$baseAuthUrl = [
Yii::$app->controller->getRoute()
];
$params = $_GET;
unset($params[$this->clientIdGetParamName]);
$baseAuthUrl = array_merge($baseAuthUrl, $params);

return $baseAuthUrl;
}

/**
* Outputs client auth link.
* @param ClientInterface $client external auth client instance.
* @param string $text link text, if not set - default value will be generated.
* @param array $htmlOptions link HTML options.
* @throws InvalidConfigException on wrong configuration.
*/
public function clientLink($client, $text = null, array $htmlOptions = [])
{
echo Html::beginTag('div', ['class' => $this->clientCssClass]);
$text = Html::tag('span', $text, ['class' => 'auth-icon ' . $client->getName()]);

if (!array_key_exists('class', $htmlOptions)) {
$htmlOptions['class'] = 'auth-link ' . $client->getName();
}

$viewOptions = $client->getViewOptions();
if (empty($viewOptions['widget'])) {
if ($this->popupMode) {
if (isset($viewOptions['popupWidth'])) {
$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
}
if (isset($viewOptions['popupHeight'])) {
$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
}
}
echo Html::a($text, $this->createClientUrl($client), $htmlOptions);
} else {
$widgetConfig = $viewOptions['widget'];
if (!isset($widgetConfig['class'])) {
throw new InvalidConfigException('Widget config "class" parameter is missing');
}
/* @var $widgetClass Widget */
$widgetClass = $widgetConfig['class'];
if (!(is_subclass_of($widgetClass, AuthChoiceItem::className()))) {
throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::className() . '"');
}
unset($widgetConfig['class']);
$widgetConfig['client'] = $client;
$widgetConfig['authChoice'] = $this;
echo $widgetClass::widget($widgetConfig);
}
echo Html::endTag('div');
}

/**
* Composes client auth URL.
* @param ClientInterface $provider external auth client instance.
* @return string auth URL.
*/
public function createClientUrl($provider)
{
$this->autoRender = false;
$url = $this->getBaseAuthUrl();
$url[$this->clientIdGetParamName] = $provider->getId();

return Url::to($url);
}

/**
* Renders the main content, which includes all external services links.
*/
protected function renderMainContent()
{
echo Html::beginTag('div', ['class' => 'row']);
foreach ($this->getClients() as $externalService) {
$this->clientLink($externalService);
}
echo Html::endTag('div');
}

/**
* Initializes the widget.
*/
public function init()
{
$view = Yii::$app->getView();
if ($this->popupMode) {
AuthChoiceAsset::register($view);
$view->registerJs("\$('#" . $this->getId() . "').authchoice();");
} else {
AuthChoiceStyleAsset::register($view);
}
$this->options['id'] = $this->getId();
echo Html::beginTag('div', $this->options);
}

/**
* Runs the widget.
*/
public function run()
{
if ($this->autoRender) {
$this->renderMainContent();
}
echo Html::endTag('div');
}
}
Loading

0 comments on commit 1023263

Please sign in to comment.