interface HasStatus
{
public const STATUS_NOT_PAID = 0;
public const STATUS_PAID = 1;
public const STATUS_CANCEL = 3;
public function getStatus(): int;
}
class StatusHelper
{
/**
* Статистический метод получения списка статусов
*
* @return array
*/
public static function getStatusList()
{
return [
HasStatus::STATUS_NOT_PAID => 'Не оплачено',
HasStatus::STATUS_PAID => 'Оплачено',
HasStatus::STATUS_CANCEL => 'Отменена',
];
}
public function getStatusName(HasStatus $entity): string
{
ArrayHelper::getValue(self::getStatusList(), $entity->getStatus());
}
}
<?php
namespace app\components;
use Adldap\Adldap;
use Adldap\Auth\BindException;
use Adldap\Auth\PasswordRequiredException;
use Adldap\Auth\UsernameRequiredException;
use Adldap\Connections\ProviderInterface;
use Adldap\Models\Group;
use Adldap\Models\Model;
use Adldap\Models\User;
use app\components\interfaces\ExternalAuthProviderInterface;
use Illuminate\Support\Collection;
use yii\base\Component;
/**
* Class LdapService
* Wrapper to work with LDAP package.
*
* @package app\components
*/
class LdapService extends Component implements ExternalAuthProviderInterface
{
/**
* Account suffix.
*
* @var string
*/
public $accountSuffix = '';
/**
* List of domain controllers.
*
* @var array
*/
public $domainControllers = [];
/**
* Base DN.
*
* @var string
*/
public $baseDn;
/**
* Domain admin user name.
*
* @var string
*/
public $adminUserName;
/**
* Domain admin password.
*
* @var string
*/
public $adminPassword;
/**
* Connection provider to the AD.
*
* @var ProviderInterface
*/
private $provider;
/**
* Attempt to login in domain.
*
* @param string $login login.
* @param string $password password.
*
* @throws BindException if there are connections errors.
* @throws PasswordRequiredException
* @throws UsernameRequiredException
*
* @return bool
*/
public function attempt(string $login, string $password): bool
{
return $this->getProvider()
->auth()
->attempt($login, $password);
}
/**
* Find user in the AD by login(account name).
*
* @param string $login login/account name.
*
* @throws \Adldap\Models\ModelNotFoundException
* @throws BindException
* @throws \InvalidArgumentException
* @return User|Model
*/
public function findUserByLogin(string $login): User
{
return $this->getProvider()
->search()
->users()
->where('samaccountname', '=', $login)
->firstOrFail();
}
/**
* Find user in the AD by full name.
*
* @param string $username users full name.
*
* @throws \Adldap\Models\ModelNotFoundException
* @throws BindException
* @throws \InvalidArgumentException
* @return User|Model
*/
public function findUserByFullName(string $username): User
{
return $this->getProvider()
->search()
->users()
->where('cn', '=', $username)
->firstOrFail();
}
/**
* Returns list of all group in the AD.
*
* @throws BindException
* @return Collection|Group[]
*/
public function getAllGroups(): Collection
{
return $this->getProvider()
->search()
->groups()
->get();
}
/**
* Returns configured AD provider.
*
* @throws BindException
* @return ProviderInterface
*/
private function getProvider(): ProviderInterface
{
if (null === $this->provider) {
$ad = new Adldap($this->getConfig());
$this->provider = $ad->connect('default');
}
return $this->provider;
}
/**
* Returns config to connect to the ldap domain.
*
* @return array
*/
private function getConfig(): array
{
return [
'default' => [
'domain_controllers' => $this->domainControllers,
'base_dn' => $this->baseDn,
'admin_username' => $this->adminUserName,
'admin_password' => $this->adminPassword,
'account_suffix' => $this->accountSuffix,
],
];
}
}
'components' => [
...
'ldap' => [
'class' => \app\components\LdapService::class,
'accountSuffix' => '@test',
'domainControllers' => ['192.168.1.1'],
'baseDn' => 'DC=admin',
'adminUserName' => 'admin',
'adminPassword' => 'password',
],
],
$user = \Yii::$app->get('ldap')->findUserByLogin('someLogin');
products
----------
id
name
...
categories
------------
id
name
...
products_categories
------------------------
product_id
category_id
<?php
namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public function init()
{
$format = 'theme/assets/global/plugins/date/locales/dater.%s.min.js';
$this->js[] = sprintf($format, \Yii::$app->language);
}
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
];
return $behaviors;
}
<?php
interface UserInterface
{
public function getFirstName();
public function getLastName();
public function getLogin();
}
/**
* Class User
*
* @property string $firstName
* @property string $lastName
* @property string $login
*/
class User extends \yii\db\ActiveRecord implements UserInterface
{
...
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
public function getLogin()
{
return $this->login;
}
}
class TestController
{
/**
* Здесь явно указываем тип переменной, которую ожидаем. Если мы передадим
* объект, который не имплиментирует нужный интерфейс, то будет ошибка.
*
* @param UserInterface $user
*/
public function actionTest(UserInterface $user)
{
$name = $user->getFirstName();
...
}
}
$form = ActiveForm::begin([
'id' => 'form-input-example',
'options' => [
'onsubmit' => 'sendAjax(this, myAction)'
],
]);
...
var myAction = function (response) {
//Делаем то, что нам нужно с ответом
console.log(response);
}
function sendAjax(form, callback) {
$.ajax({
method: 'post',
url: '/test',
dataType: 'json',
data: $(form).serialize()
}).done(function (response) {
callback(response);
})
//Возвращаем false чтобы форма не отправилась
return false;
}
try {
//Тут что то делаем с сокетом
fsockopen();
} catch (ErrorException $e) {
//Что то пошло не так - обработаем исключение, например, так:
echo $e->getMessage();
}
<?php
namespace app\modules\pages\config
class Config
{
...
}