添加共享
This commit is contained in:
1 parent
c3ff86d2c1
commit
aaac88f323
10 files changed
+2456
-84
No files matched your search
+282
-9
@@ -99,8 +99,8 @@ function user_count($q)
|
||||
function user_insert($data)
|
||||
{
|
||||
db_run(
|
||||
'INSERT INTO users (id, username, password_hash, role, disabled, must_change_password, created_at, last_login_at, login_count)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
'INSERT INTO users (id, username, password_hash, role, disabled, must_change_password, protected, created_at, last_login_at, login_count)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
array(
|
||||
$data['id'],
|
||||
$data['username'],
|
||||
@@ -108,6 +108,7 @@ function user_insert($data)
|
||||
isset($data['role']) ? $data['role'] : 'user',
|
||||
!empty($data['disabled']) ? 1 : 0,
|
||||
!empty($data['must_change_password']) ? 1 : 0,
|
||||
!empty($data['protected']) ? 1 : 0,
|
||||
isset($data['created_at']) ? $data['created_at'] : now_iso(),
|
||||
isset($data['last_login_at']) ? $data['last_login_at'] : null,
|
||||
isset($data['login_count']) ? (int) $data['login_count'] : 0,
|
||||
@@ -146,6 +147,120 @@ function user_touch_login($id)
|
||||
db_run('UPDATE users SET last_login_at = ?, login_count = login_count + 1 WHERE id = ?', array(now_iso(), $id));
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* 用户分组
|
||||
* ============================================================ */
|
||||
function group_list()
|
||||
{
|
||||
return db_all('SELECT * FROM user_groups ORDER BY created_at ASC, name ASC');
|
||||
}
|
||||
|
||||
function group_find($id)
|
||||
{
|
||||
return db_one('SELECT * FROM user_groups WHERE id = ?', array($id));
|
||||
}
|
||||
|
||||
function group_find_by_name($name)
|
||||
{
|
||||
return db_one('SELECT * FROM user_groups WHERE name = ?', array($name));
|
||||
}
|
||||
|
||||
function group_insert($data)
|
||||
{
|
||||
db_run(
|
||||
'INSERT INTO user_groups (id, name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?)',
|
||||
array(
|
||||
$data['id'],
|
||||
$data['name'],
|
||||
isset($data['description']) ? $data['description'] : '',
|
||||
isset($data['created_at']) ? $data['created_at'] : now_iso(),
|
||||
isset($data['updated_at']) ? $data['updated_at'] : now_iso(),
|
||||
)
|
||||
);
|
||||
return $data['id'];
|
||||
}
|
||||
|
||||
function group_update($id, $fields)
|
||||
{
|
||||
$allowed = array('name', 'description');
|
||||
$sets = array();
|
||||
$params = array();
|
||||
foreach ($allowed as $f) {
|
||||
if (array_key_exists($f, $fields)) {
|
||||
$sets[] = $f . ' = ?';
|
||||
$params[] = $fields[$f];
|
||||
}
|
||||
}
|
||||
if (!$sets) {
|
||||
return false;
|
||||
}
|
||||
$sets[] = 'updated_at = ?';
|
||||
$params[] = now_iso();
|
||||
$params[] = $id;
|
||||
db_run('UPDATE user_groups SET ' . implode(', ', $sets) . ' WHERE id = ?', $params);
|
||||
return true;
|
||||
}
|
||||
|
||||
function group_delete($id)
|
||||
{
|
||||
db_run('DELETE FROM user_groups WHERE id = ?', array($id));
|
||||
db_run('DELETE FROM user_group_members WHERE group_id = ?', array($id));
|
||||
db_run("DELETE FROM topo_shares WHERE target_type = 'group' AND target_id = ?", array($id));
|
||||
}
|
||||
|
||||
function group_members($gid)
|
||||
{
|
||||
return db_all(
|
||||
'SELECT u.id, u.username FROM user_group_members m JOIN users u ON u.id = m.user_id
|
||||
WHERE m.group_id = ? ORDER BY u.username ASC',
|
||||
array($gid)
|
||||
);
|
||||
}
|
||||
|
||||
function group_member_ids($gid)
|
||||
{
|
||||
$out = array();
|
||||
foreach (db_all('SELECT user_id FROM user_group_members WHERE group_id = ?', array($gid)) as $r) {
|
||||
$out[] = $r['user_id'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function group_set_members($gid, $userIds)
|
||||
{
|
||||
db_run('DELETE FROM user_group_members WHERE group_id = ?', array($gid));
|
||||
$seen = array();
|
||||
foreach ((array) $userIds as $uid) {
|
||||
$uid = (string) $uid;
|
||||
if ($uid === '' || isset($seen[$uid])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$uid] = true;
|
||||
db_run(
|
||||
'INSERT INTO user_group_members (group_id, user_id, created_at) VALUES (?, ?, ?)',
|
||||
array($gid, $uid, now_iso())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function group_ids_of_user($uid)
|
||||
{
|
||||
$out = array();
|
||||
foreach (db_all('SELECT group_id FROM user_group_members WHERE user_id = ?', array($uid)) as $r) {
|
||||
$out[] = $r['group_id'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function group_member_counts()
|
||||
{
|
||||
$map = array();
|
||||
foreach (db_all('SELECT group_id, COUNT(*) AS c FROM user_group_members GROUP BY group_id') as $r) {
|
||||
$map[$r['group_id']] = (int) $r['c'];
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* 会话
|
||||
* ============================================================ */
|
||||
@@ -187,11 +302,21 @@ function topo_find($id)
|
||||
|
||||
function topo_list_for_user($user)
|
||||
{
|
||||
/* 主应用「我的拓扑」仅返回:当前用户创建的 + 公开的。
|
||||
/* 主应用 / 用户后台返回:本人创建的 + 公开的 + 通过成员/分组共享且已开启共享模式的。
|
||||
全部拓扑(含他人私有)只能在管理后台查看,见 topo_admin_search()。 */
|
||||
return db_all(
|
||||
'SELECT * FROM topologies WHERE owner_id = ? OR visibility = ? ORDER BY updated_at DESC',
|
||||
array($user['id'], 'public')
|
||||
"SELECT DISTINCT t.* FROM topologies t
|
||||
LEFT JOIN topo_shares s ON s.topo_id = t.id
|
||||
WHERE t.owner_id = ?
|
||||
OR t.visibility = 'public'
|
||||
OR (t.share_mode = 1 AND (
|
||||
(s.target_type = 'user' AND s.target_id = ?)
|
||||
OR (s.target_type = 'group' AND s.target_id IN (
|
||||
SELECT group_id FROM user_group_members WHERE user_id = ?
|
||||
))
|
||||
))
|
||||
ORDER BY t.updated_at DESC",
|
||||
array($user['id'], $user['id'], $user['id'])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -276,12 +401,12 @@ function topo_update($id, $data, $nodeCount, $edgeCount, $updatedAt, $name = nul
|
||||
{
|
||||
if ($name !== null && $name !== '') {
|
||||
db_run(
|
||||
'UPDATE topologies SET data = ?, node_count = ?, edge_count = ?, updated_at = ?, name = ? WHERE id = ?',
|
||||
'UPDATE topologies SET data = ?, node_count = ?, edge_count = ?, updated_at = ?, rev = rev + 1, name = ? WHERE id = ?',
|
||||
array($data, (int) $nodeCount, (int) $edgeCount, $updatedAt, $name, $id)
|
||||
);
|
||||
} else {
|
||||
db_run(
|
||||
'UPDATE topologies SET data = ?, node_count = ?, edge_count = ?, updated_at = ? WHERE id = ?',
|
||||
'UPDATE topologies SET data = ?, node_count = ?, edge_count = ?, updated_at = ?, rev = rev + 1 WHERE id = ?',
|
||||
array($data, (int) $nodeCount, (int) $edgeCount, $updatedAt, $id)
|
||||
);
|
||||
}
|
||||
@@ -294,12 +419,146 @@ function topo_delete($id)
|
||||
|
||||
function topo_set_visibility($id, $vis, $updatedAt)
|
||||
{
|
||||
db_run('UPDATE topologies SET visibility = ?, updated_at = ? WHERE id = ?', array($vis, $updatedAt, $id));
|
||||
db_run('UPDATE topologies SET visibility = ?, updated_at = ?, rev = rev + 1 WHERE id = ?', array($vis, $updatedAt, $id));
|
||||
}
|
||||
|
||||
function topo_set_name($id, $name, $updatedAt)
|
||||
{
|
||||
db_run('UPDATE topologies SET name = ?, updated_at = ? WHERE id = ?', array($name, $updatedAt, $id));
|
||||
db_run('UPDATE topologies SET name = ?, updated_at = ?, rev = rev + 1 WHERE id = ?', array($name, $updatedAt, $id));
|
||||
}
|
||||
|
||||
function topo_set_share_mode($id, $on, $updatedAt)
|
||||
{
|
||||
db_run('UPDATE topologies SET share_mode = ?, updated_at = ?, rev = rev + 1 WHERE id = ?', array($on ? 1 : 0, $updatedAt, $id));
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* 拓扑共享
|
||||
* ============================================================ */
|
||||
function share_list_by_topo($topoId)
|
||||
{
|
||||
return db_all('SELECT * FROM topo_shares WHERE topo_id = ? ORDER BY created_at ASC', array($topoId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 整体替换某拓扑的共享列表(事务)
|
||||
*/
|
||||
function share_replace($topoId, $rows, $by)
|
||||
{
|
||||
$pdo = db();
|
||||
$pdo->beginTransaction();
|
||||
try {
|
||||
db_run('DELETE FROM topo_shares WHERE topo_id = ?', array($topoId));
|
||||
foreach ((array) $rows as $r) {
|
||||
db_run(
|
||||
'INSERT INTO topo_shares (id, topo_id, target_type, target_id, permission, created_by, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
array(
|
||||
gen_id('s'),
|
||||
$topoId,
|
||||
$r['target_type'],
|
||||
$r['target_id'],
|
||||
$r['permission'],
|
||||
$by,
|
||||
now_iso(),
|
||||
)
|
||||
);
|
||||
}
|
||||
$pdo->commit();
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算用户对拓扑的有效权限:owner | edit | view | none
|
||||
*/
|
||||
function topo_permission_for($row, $user)
|
||||
{
|
||||
if (!$row || !$user) {
|
||||
return 'none';
|
||||
}
|
||||
if (isset($user['role']) && $user['role'] === 'admin') {
|
||||
return 'owner';
|
||||
}
|
||||
if ($row['owner_id'] === $user['id']) {
|
||||
return 'owner';
|
||||
}
|
||||
$best = 'none';
|
||||
if (!empty($row['share_mode'])) {
|
||||
$gids = group_ids_of_user($user['id']);
|
||||
foreach (share_list_by_topo($row['id']) as $s) {
|
||||
$hit = false;
|
||||
if ($s['target_type'] === 'user' && $s['target_id'] === $user['id']) {
|
||||
$hit = true;
|
||||
} elseif ($s['target_type'] === 'group' && in_array($s['target_id'], $gids, true)) {
|
||||
$hit = true;
|
||||
}
|
||||
if (!$hit) {
|
||||
continue;
|
||||
}
|
||||
if ($s['permission'] === 'edit') {
|
||||
return 'edit';
|
||||
}
|
||||
if ($s['permission'] === 'view') {
|
||||
$best = 'view';
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($best === 'view') {
|
||||
return 'view';
|
||||
}
|
||||
if ($row['visibility'] === 'public') {
|
||||
return 'view';
|
||||
}
|
||||
return 'none';
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* 在线状态(协同)
|
||||
* ============================================================ */
|
||||
function presence_touch($topoId, $user)
|
||||
{
|
||||
$now = time();
|
||||
$exists = db_one('SELECT topo_id FROM topo_presence WHERE topo_id = ? AND user_id = ?', array($topoId, $user['id']));
|
||||
if ($exists) {
|
||||
db_run(
|
||||
'UPDATE topo_presence SET username = ?, last_seen = ? WHERE topo_id = ? AND user_id = ?',
|
||||
array($user['username'], $now, $topoId, $user['id'])
|
||||
);
|
||||
} else {
|
||||
db_run(
|
||||
'INSERT INTO topo_presence (topo_id, user_id, username, last_seen) VALUES (?, ?, ?, ?)',
|
||||
array($topoId, $user['id'], $user['username'], $now)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function presence_list($topoId, $activeWithin)
|
||||
{
|
||||
$cut = time() - (int) $activeWithin;
|
||||
return db_all(
|
||||
'SELECT user_id, username, last_seen FROM topo_presence WHERE topo_id = ? AND last_seen >= ? ORDER BY last_seen DESC',
|
||||
array($topoId, $cut)
|
||||
);
|
||||
}
|
||||
|
||||
function presence_remove($topoId, $userId)
|
||||
{
|
||||
db_run('DELETE FROM topo_presence WHERE topo_id = ? AND user_id = ?', array($topoId, $userId));
|
||||
}
|
||||
|
||||
function presence_purge($beforeTs)
|
||||
{
|
||||
db_run('DELETE FROM topo_presence WHERE last_seen < ?', array((int) $beforeTs));
|
||||
}
|
||||
|
||||
function topo_rev($id)
|
||||
{
|
||||
return (int) db_val('SELECT rev FROM topologies WHERE id = ?', array($id));
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
@@ -463,6 +722,7 @@ function ensure_seed()
|
||||
migrate_legacy_json();
|
||||
|
||||
if ((int) db_val('SELECT COUNT(*) FROM users') > 0) {
|
||||
protect_configured_admin();
|
||||
return;
|
||||
}
|
||||
$cfg = app_config();
|
||||
@@ -475,5 +735,18 @@ function ensure_seed()
|
||||
'disabled' => 0,
|
||||
'must_change_password' => 1,
|
||||
'created_at' => now_iso(),
|
||||
'protected' => 1,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认管理员(配置文件中的 default_admin)始终置为受保护账号,不可被其他用户改动
|
||||
*/
|
||||
function protect_configured_admin()
|
||||
{
|
||||
$cfg = app_config();
|
||||
$name = isset($cfg['default_admin']['username']) ? $cfg['default_admin']['username'] : 'admin';
|
||||
if ($name !== '' && $name !== null) {
|
||||
db_run('UPDATE users SET protected = 1 WHERE username = ?', array($name));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user