You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
496 lines
16 KiB
496 lines
16 KiB
3 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* @Author: witersen
|
||
|
* @Date: 2022-04-24 23:37:05
|
||
|
* @LastEditors: witersen
|
||
3 years ago
|
* @LastEditTime: 2022-05-07 14:19:26
|
||
3 years ago
|
* @Description: QQ:1801168257
|
||
|
*/
|
||
3 years ago
|
|
||
3 years ago
|
namespace app\service;
|
||
3 years ago
|
|
||
3 years ago
|
class Svngroup extends Base
|
||
3 years ago
|
{
|
||
3 years ago
|
function __construct()
|
||
|
{
|
||
|
parent::__construct();
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 将SVN分组数据同步到数据库
|
||
|
*
|
||
|
* 同时同步每个分组包含的用户数量
|
||
|
*/
|
||
3 years ago
|
private function SyncGroupToDb()
|
||
3 years ago
|
{
|
||
3 years ago
|
$svnAndGroupList = $this->SVNAdminGroup->GetSvnGroupUserAndGroupList($this->authzContent);
|
||
3 years ago
|
|
||
|
if ($svnAndGroupList == 0) {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
|
||
|
$svnGroupList = FunArrayColumn($svnAndGroupList, 'groupName');
|
||
|
|
||
|
$dbGroupPassList = $this->database->select('svn_groups', [
|
||
|
'svn_group_id',
|
||
|
'svn_group_name',
|
||
|
'svn_group_note',
|
||
|
'include_user_count',
|
||
|
'include_group_count'
|
||
|
]);
|
||
|
|
||
|
$combinArray = array_combine(FunArrayColumn($svnAndGroupList, 'groupName'), FunArrayColumn($svnAndGroupList, 'include'));
|
||
|
|
||
|
foreach ($dbGroupPassList as $key => $value) {
|
||
|
if (!in_array($value['svn_group_name'], $svnGroupList)) {
|
||
|
$this->database->delete('svn_groups', [
|
||
|
'svn_group_name' => $value['svn_group_name']
|
||
|
]);
|
||
|
} else {
|
||
|
//更新数量
|
||
|
$this->database->update('svn_groups', [
|
||
|
'include_user_count' => count($combinArray[$value['svn_group_name']]['users']),
|
||
|
'include_group_count' => count($combinArray[$value['svn_group_name']]['groups']),
|
||
|
], [
|
||
|
'svn_group_name' => $value['svn_group_name']
|
||
|
]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($svnGroupList as $key => $value) {
|
||
|
if (!in_array($value, FunArrayColumn($dbGroupPassList, 'svn_group_name'))) {
|
||
|
$this->database->insert('svn_groups', [
|
||
|
'svn_group_name' => $value,
|
||
|
'include_user_count' => count($svnAndGroupList[$key]['include']['users']),
|
||
|
'include_group_count' => count($svnAndGroupList[$key]['include']['groups']),
|
||
|
'svn_group_note' => '',
|
||
|
]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取所有的分组列表
|
||
|
*/
|
||
3 years ago
|
public function GetAllGroupList()
|
||
3 years ago
|
{
|
||
3 years ago
|
$svnGroupList = $this->SVNAdminGroup->GetSvnGroupList($this->authzContent);
|
||
3 years ago
|
if ($svnGroupList == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
} else {
|
||
|
$newArray = [];
|
||
|
foreach ($svnGroupList as $key => $value) {
|
||
|
array_push($newArray, [
|
||
|
'groupName' => $value
|
||
|
]);
|
||
|
}
|
||
3 years ago
|
return message(200, 1, '成功', $newArray);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取带有分页的分组列表
|
||
|
*/
|
||
3 years ago
|
public function GetGroupList()
|
||
3 years ago
|
{
|
||
|
//同步
|
||
|
$this->SyncGroupToDb();
|
||
|
|
||
3 years ago
|
$pageSize = $this->payload['pageSize'];
|
||
|
$currentPage = $this->payload['currentPage'];
|
||
|
$searchKeyword = trim($this->payload['searchKeyword']);
|
||
3 years ago
|
|
||
|
//分页
|
||
|
$begin = $pageSize * ($currentPage - 1);
|
||
|
|
||
|
$list = $this->database->select('svn_groups', [
|
||
|
'svn_group_id',
|
||
|
'svn_group_name',
|
||
|
'svn_group_note',
|
||
|
'include_user_count',
|
||
|
'include_group_count'
|
||
|
], [
|
||
|
'AND' => [
|
||
|
'OR' => [
|
||
|
'svn_group_name[~]' => $searchKeyword,
|
||
|
'svn_group_note[~]' => $searchKeyword,
|
||
|
],
|
||
|
],
|
||
|
'LIMIT' => [$begin, $pageSize],
|
||
|
'ORDER' => [
|
||
3 years ago
|
$this->payload['sortName'] => strtoupper($this->payload['sortType'])
|
||
3 years ago
|
]
|
||
|
]);
|
||
|
|
||
|
$total = $this->database->count('svn_groups', [
|
||
|
'svn_group_id'
|
||
|
], [
|
||
|
'AND' => [
|
||
|
'OR' => [
|
||
|
'svn_group_name[~]' => $searchKeyword,
|
||
|
'svn_group_note[~]' => $searchKeyword,
|
||
|
],
|
||
|
],
|
||
|
]);
|
||
|
|
||
3 years ago
|
return message(200, 1, '成功', [
|
||
3 years ago
|
'data' => $list,
|
||
|
'total' => $total
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 编辑分组备注信息
|
||
|
*/
|
||
3 years ago
|
public function EditGroupNote()
|
||
3 years ago
|
{
|
||
|
$this->database->update('svn_groups', [
|
||
3 years ago
|
'svn_group_note' => $this->payload['svn_group_note']
|
||
3 years ago
|
], [
|
||
3 years ago
|
'svn_group_name' => $this->payload['svn_group_name']
|
||
3 years ago
|
]);
|
||
|
|
||
3 years ago
|
return message(200, 1, '已保存');
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 新建SVN分组
|
||
|
*/
|
||
3 years ago
|
public function CreateGroup()
|
||
3 years ago
|
{
|
||
|
//检查分组名是否合法
|
||
3 years ago
|
$checkResult = $this->checkService->CheckRepGroup($this->payload['svn_group_name']);
|
||
|
if ($checkResult['status'] != 1) {
|
||
|
return message($checkResult['code'], $checkResult['status'], $checkResult['message'], $checkResult['data']);
|
||
|
}
|
||
3 years ago
|
|
||
|
//检查用户是否已存在
|
||
3 years ago
|
$result = $this->SVNAdminGroup->AddSvnGroup($this->authzContent, $this->payload['svn_group_name']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组已存在');
|
||
3 years ago
|
}
|
||
|
|
||
|
//写入配置文件
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
|
//写入数据库
|
||
|
$this->database->insert('svn_groups', [
|
||
3 years ago
|
'svn_group_name' => $this->payload['svn_group_name'],
|
||
3 years ago
|
'include_user_count' => 0,
|
||
|
'include_group_count' => 0,
|
||
|
'svn_group_note' => ''
|
||
|
]);
|
||
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 删除SVN分组
|
||
|
*/
|
||
3 years ago
|
public function DelGroup()
|
||
3 years ago
|
{
|
||
|
//从authz文件删除
|
||
3 years ago
|
$result = $this->SVNAdminGroup->DelSvnGroup($this->authzContent, $this->payload['svn_group_name']);
|
||
3 years ago
|
|
||
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
|
//从数据库删除
|
||
|
$this->database->delete('svn_groups', [
|
||
3 years ago
|
'svn_group_name' => $this->payload['svn_group_name']
|
||
3 years ago
|
]);
|
||
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 修改SVN分组的名称
|
||
|
*/
|
||
3 years ago
|
public function EditGroupName()
|
||
3 years ago
|
{
|
||
|
//新分组名称是否合法
|
||
3 years ago
|
$checkResult = $this->checkService->CheckRepGroup($this->payload['groupNameNew']);
|
||
|
if ($checkResult['status'] != 1) {
|
||
|
return message($checkResult['code'], $checkResult['status'], $checkResult['message'], $checkResult['data']);
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
$svnGroupList = $this->SVNAdminGroup->GetSvnGroupList($this->authzContent);
|
||
3 years ago
|
|
||
|
//旧分组是否存在
|
||
3 years ago
|
if (!in_array($this->payload['groupNameOld'], $svnGroupList)) {
|
||
3 years ago
|
return message(200, 0, '当前分组不存在');
|
||
3 years ago
|
}
|
||
|
|
||
|
//新分组名称是否冲突
|
||
3 years ago
|
if (in_array($this->payload['groupNameNew'], $svnGroupList)) {
|
||
3 years ago
|
return message(200, 0, '要修改的分组名称已经存在');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
$result = $this->SVNAdminGroup->UpdSvnGroup($this->authzContent, $this->payload['groupNameOld'], $this->payload['groupNameNew']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取SVN分组的用户成员和分组成员
|
||
|
*/
|
||
3 years ago
|
public function GetGroupMember()
|
||
3 years ago
|
{
|
||
3 years ago
|
$memberUserList = $this->SVNAdminGroup->GetSvnUserListByGroup($this->authzContent, $this->payload['svn_group_name']);
|
||
3 years ago
|
|
||
3 years ago
|
$memberGroupList = $this->SVNAdminGroup->GetSvnGroupListByGroup($this->authzContent, $this->payload['svn_group_name']);
|
||
3 years ago
|
|
||
3 years ago
|
$allGroupList = $this->SVNAdminGroup->GetSvnGroupList($this->authzContent);
|
||
3 years ago
|
|
||
3 years ago
|
$allUserList = $this->SVNAdminUser->GetSvnUserList($this->passwdContent);
|
||
3 years ago
|
|
||
|
if ($memberUserList == '0' || $memberGroupList == '0' || $allGroupList == '0' || $allUserList == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($memberUserList == '1' || $memberGroupList == '1' || $allUserList == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
|
||
|
$group1 = [];
|
||
|
foreach ($allGroupList as $key => $value) {
|
||
|
if (in_array($value, $memberGroupList)) {
|
||
|
array_push($group1, [
|
||
|
'groupName' => $value,
|
||
|
'isMember' => true
|
||
|
]);
|
||
|
} else {
|
||
|
array_push($group1, [
|
||
|
'groupName' => $value,
|
||
|
'isMember' => false
|
||
|
]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//排序
|
||
3 years ago
|
// array_multisort(FunArrayColumn($group1, 'isMember'), SORT_DESC, $group1);
|
||
3 years ago
|
|
||
|
$group2 = [];
|
||
|
foreach ($allUserList as $key => $value) {
|
||
|
if (in_array($value['userName'], $memberUserList)) {
|
||
|
array_push($group2, [
|
||
|
'userName' => $value['userName'],
|
||
|
'isMember' => true,
|
||
|
'disabled' => $value['disabled']
|
||
|
]);
|
||
|
} else {
|
||
|
array_push($group2, [
|
||
|
'userName' => $value['userName'],
|
||
|
'isMember' => false,
|
||
|
'disabled' => $value['disabled']
|
||
|
]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//排序
|
||
3 years ago
|
// array_multisort(FunArrayColumn($group2, 'isMember'), SORT_DESC, $group2);
|
||
3 years ago
|
|
||
3 years ago
|
return message(200, 1, '成功', [
|
||
3 years ago
|
'userList' => $group2,
|
||
|
'groupList' => $group1
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将用户添加为SVN分组的成员
|
||
|
*/
|
||
3 years ago
|
public function GroupAddUser()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->SVNAdminGroup->AddSvnGroupUser($this->authzContent, $this->payload['svn_group_name'], $this->payload['svn_user_name']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
if ($result == '2') {
|
||
3 years ago
|
return message(200, 0, '要添加的用户已存在该分组');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 将用户从SVN分组的成员移除
|
||
|
*/
|
||
3 years ago
|
public function GroupRemoveUser()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->SVNAdminGroup->DelSvnGroupUser($this->authzContent, $this->payload['svn_group_name'], $this->payload['svn_user_name']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
if ($result == '2') {
|
||
3 years ago
|
return message(200, 0, '要删除的用户不在该分组');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 将分组添加为SVN分组的成员
|
||
|
*/
|
||
3 years ago
|
public function GroupAddGroup()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->SVNAdminGroup->AddSvnGroupGroup($this->authzContent, $this->payload['svn_group_name'], $this->payload['svn_group_name_add']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
if ($result == '2') {
|
||
3 years ago
|
return message(200, 0, '要添加的分组已存在该分组');
|
||
3 years ago
|
}
|
||
|
if ($result == '3') {
|
||
3 years ago
|
return message(200, 0, '不能添加本身');
|
||
3 years ago
|
}
|
||
|
|
||
|
//检查是否存在分组循环嵌套问题
|
||
|
//获取分组所在的所有分组
|
||
3 years ago
|
$groupGroupList = $this->GetSvnGroupAllGroupList($this->payload['svn_group_name']);
|
||
3 years ago
|
|
||
3 years ago
|
if (in_array($this->payload['svn_group_name_add'], $groupGroupList)) {
|
||
3 years ago
|
return message(200, 0, '存在分组循环嵌套的情况');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 将分组从SVN分组的成员移除
|
||
|
*/
|
||
3 years ago
|
public function GroupRemoveGroup()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->SVNAdminGroup->DelSvnGroupGroup($this->authzContent, $this->payload['svn_group_name'], $this->payload['svn_group_name_del']);
|
||
3 years ago
|
if ($result == '0') {
|
||
3 years ago
|
return message(200, 0, '文件格式错误(不存在[groups]标识)');
|
||
3 years ago
|
}
|
||
|
if ($result == '1') {
|
||
3 years ago
|
return message(200, 0, '分组不存在');
|
||
3 years ago
|
}
|
||
|
if ($result == '2') {
|
||
3 years ago
|
return message(200, 0, '要删除的分组不在该分组');
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
FunShellExec('echo \'' . $result . '\' > ' . $this->config_svn['svn_authz_file']);
|
||
3 years ago
|
|
||
3 years ago
|
return message();
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 获取用户所在的所有分组
|
||
|
*
|
||
|
* 包括直接包含关系 如
|
||
|
* group1=user1
|
||
|
*
|
||
|
* 和间接包含关系 如
|
||
|
* group1=user1
|
||
|
* group2=@group1
|
||
|
* group3=@group2
|
||
|
* group4=@group3
|
||
|
*/
|
||
|
public function GetSvnUserAllGroupList($userName)
|
||
|
{
|
||
|
$authzContent = $this->authzContent;
|
||
|
|
||
|
//所有的分组列表
|
||
|
$allGroupList = $this->SVNAdminGroup->GetSvnGroupList($authzContent);
|
||
|
|
||
|
//用户所在的分组列表
|
||
|
$userGroupList = $this->SVNAdminUser->GetSvnUserGroupList($authzContent, $userName);
|
||
|
|
||
|
//剩余的分组列表
|
||
|
$leftGroupList = array_diff($allGroupList, $userGroupList);
|
||
|
|
||
|
//循环匹配 直到匹配到与该用户相关的有权限的用户组为止
|
||
|
loop:
|
||
|
$userGroupListBack = $userGroupList;
|
||
|
foreach ($userGroupList as $group1) {
|
||
|
$newList = $this->SVNAdminGroup->GetSvnGroupGroupList($authzContent, $group1);
|
||
|
foreach ($leftGroupList as $key2 => $group2) {
|
||
|
if (in_array($group2, $newList)) {
|
||
|
array_push($userGroupList, $group2);
|
||
|
unset($leftGroupList[$key2]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if ($userGroupList != $userGroupListBack) {
|
||
|
goto loop;
|
||
|
}
|
||
|
|
||
|
return $userGroupList;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 获取分组所在的所有分组
|
||
|
*
|
||
|
* 包括直接包含关系 如
|
||
|
* group2=@group1
|
||
|
*
|
||
|
* 和间接包含关系 如
|
||
|
* group2=@group1
|
||
|
* group3=@group2
|
||
|
* group4=@group3
|
||
|
*/
|
||
3 years ago
|
private function GetSvnGroupAllGroupList($groupName)
|
||
3 years ago
|
{
|
||
|
$parentGroupName = $groupName;
|
||
|
|
||
3 years ago
|
$authzContent = $this->authzContent;
|
||
3 years ago
|
|
||
|
//所有的分组列表
|
||
3 years ago
|
$allGroupList = $this->SVNAdminGroup->GetSvnGroupList($authzContent);
|
||
3 years ago
|
|
||
|
//分组所在的分组列表
|
||
3 years ago
|
$groupGroupList = $this->SVNAdminGroup->GetSvnGroupGroupList($authzContent, $parentGroupName);
|
||
3 years ago
|
|
||
|
//剩余的分组列表
|
||
|
$leftGroupList = array_diff($allGroupList, $groupGroupList);
|
||
|
|
||
|
//循环匹配
|
||
|
loop:
|
||
|
$userGroupListBack = $groupGroupList;
|
||
|
foreach ($groupGroupList as $group1) {
|
||
3 years ago
|
$newList = $this->SVNAdminGroup->GetSvnGroupGroupList($authzContent, $group1);
|
||
3 years ago
|
foreach ($leftGroupList as $key2 => $group2) {
|
||
3 years ago
|
if (in_array($group2, $newList)) {
|
||
|
array_push($groupGroupList, $group2);
|
||
3 years ago
|
unset($leftGroupList[$key2]);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
if ($groupGroupList != $userGroupListBack) {
|
||
|
goto loop;
|
||
|
}
|
||
|
|
||
|
return $groupGroupList;
|
||
|
}
|
||
|
}
|