146 lines
4.6 KiB
PHP
146 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use app\models\register\Partner;
|
|
use app\services\Auth;
|
|
use yii\base\BaseObject;
|
|
|
|
/**
|
|
* Class UserAdminCms
|
|
* @package app\models
|
|
* @property string $email_login
|
|
* @property $phone;
|
|
* @property string $first_name
|
|
* @property string $middle_name
|
|
* @property string $last_name
|
|
* @property string $science_degree
|
|
* @property string $presentation
|
|
* @property UserPartnerR[] $userRights
|
|
* @property CmsRoles $cmsRole
|
|
* @property int $parent_id
|
|
* @property int $partner_id
|
|
* @property Partner $partner
|
|
* @property int $role_id
|
|
* @property string $position
|
|
* @property string $open_id
|
|
*/
|
|
class UserPartner extends _Base
|
|
{
|
|
|
|
public function getPartner()
|
|
{
|
|
return $this->hasOne(Partner::class, ['id' => 'partner_id']);
|
|
}
|
|
|
|
public function getUserRights()
|
|
{
|
|
return $this->hasMany(UserPartnerR::class, ['user_id' => 'id']);
|
|
}
|
|
|
|
public function getCmsRole()
|
|
{
|
|
return $this->hasOne(CmsRoles::class, ['id' => 'role_id']);
|
|
}
|
|
|
|
public function getRightsIds($id = null)
|
|
{
|
|
$rightsIds = [];
|
|
foreach ($this->userRights as $userRight) {
|
|
if($userRight->right && !in_array($userRight->right_id, $rightsIds))
|
|
$rightsIds[] = $userRight->right_id;
|
|
}
|
|
if ($id) {
|
|
return in_array($id, $rightsIds);
|
|
} else {
|
|
return $rightsIds;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public static function getRightList(UserPartner $model, $readOnly = null)
|
|
{
|
|
list($checkedList, $checkList, $readOnlyList) = ["", "", ""];
|
|
|
|
foreach (CmsRights::find()->where(['is_active' => 1])->all() as $item) {
|
|
if (in_array($item->id, $model->getRightsIds())) {
|
|
$checkedList .= '<label data-name="' . $item->name . '"><input checked name="rights[]" type="checkbox" value="' . $item->id . '">' . $item->name . '</label>';
|
|
$readOnlyList .= '<div>' . $item->name . '</div>';
|
|
}
|
|
}
|
|
foreach (CmsRights::find()->where(['is_active' => 1])->all() as $item) {
|
|
if (!in_array($item->id, $model->getRightsIds()))
|
|
$checkList .= '<label data-name="' . $item->name . '"><input name="rights[]" type="checkbox" value="' . $item->id . '">' . $item->name . '</label>';
|
|
}
|
|
|
|
if ($readOnly)
|
|
return $readOnlyList;
|
|
|
|
return '<div style="width: 100%; background: #f8f8f8; margin: 3px 0; padding: 3px 0; border: 1px solid var(--base-background-ultra-bright)">' . $checkedList . '</div>' . $checkList;
|
|
}
|
|
|
|
public function updateRights($rightsIds)
|
|
{
|
|
list($create_list, $delete_list) = [[], []];
|
|
|
|
//Delete OLD
|
|
foreach ($this->getRightsIds() as $rightId) {
|
|
if (!in_array($rightId, $rightsIds))
|
|
$delete_list[] = $rightId;
|
|
}
|
|
$deleteUserCmsR = UserPartnerR::find()->where(['user_id' => $this->id])->andWhere(['IN', 'right_id', $delete_list])->all();
|
|
foreach ($deleteUserCmsR as $cmsUserRModel)
|
|
$cmsUserRModel->delete();
|
|
|
|
//Create new
|
|
foreach ($rightsIds as $currentId) {
|
|
if (!in_array($currentId, $this->getRightsIds()))
|
|
$create_list[] = $currentId;
|
|
}
|
|
|
|
foreach ($create_list as $rightId) {
|
|
$cmsUserRModel = new UserPartnerR();
|
|
$cmsUserRModel->user_id = $this->id;
|
|
$cmsUserRModel->right_id = $rightId;
|
|
$cmsUserRModel->save();
|
|
}
|
|
}
|
|
|
|
public static function prepareRegisterUser($userinfo, $role_id)
|
|
{
|
|
$userPartner = UserPartner::find()->where(['open_id' => $userinfo->sub])->one();
|
|
if (!$userPartner) {
|
|
$userPartner = new UserPartner();
|
|
}
|
|
$role = CmsRoles::findOne($role_id);
|
|
if (!$role)
|
|
die('Role with ID:' . $role_id . ' is missing');
|
|
$userPartner->open_id = $userinfo->sub;
|
|
$userPartner->email_login = $userinfo->email ?? null;
|
|
$userPartner->first_name = $userinfo->given_name ?? null;
|
|
$userPartner->last_name = $userinfo->family_name ?? null;
|
|
$userPartner->partner_id = $userinfo->partner_id;
|
|
$userPartner->role_id = $role->id;
|
|
$userPartner->save();
|
|
$rights = [];
|
|
|
|
if ($role_id == 1)
|
|
$rights = [1, 2, 4, 5];
|
|
if ($role_id == 2)
|
|
$rights = [1, 2];
|
|
|
|
foreach ($rights as $rightId) {
|
|
$upr = new UserPartnerR();
|
|
$upr->user_id = $userPartner->id;
|
|
$upr->right_id = $rightId;
|
|
$upr->save();
|
|
}
|
|
|
|
Auth::userPartnerLogin($userPartner->id);
|
|
header('Location: /partner/index/dashboard/');
|
|
exit;
|
|
}
|
|
}
|