Basic group management support. Lots more to do.

This commit is contained in:
Richard Underwood 2016-10-04 09:25:32 +01:00
parent 1aa0f0bbca
commit 417e9ca848
5 changed files with 168 additions and 0 deletions

View file

@ -22,6 +22,17 @@ function get_group_info($name) {
return $groupinfo;
}
function get_group_name($id) {
$db = get_db();
$q = $db->prepare('SELECT * FROM groups WHERE id = ?');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$r = $q->execute();
$ret = $r->fetchArray(SQLITE3_NUM);
$db->close();
return $ret[0];
}
function group_exists($name) {
return (bool) get_group_info($name);
}
@ -85,4 +96,60 @@ function valid_group($name) {
return ( bool ) preg_match( "/^[a-z0-9@_.-]+$/i" , $name );
}
function get_group_members($id) {
$db = get_db();
$q = $db->prepare('SELECT groupmembers.id,users.emailaddress AS user FROM groupmembers,users WHERE "group" = ? AND groupmembers.user = users.id');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$result = $q->execute();
$ret = array();
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
array_push($ret, $row);
}
return $ret;
}
// move to misc?
function get_user_id($user) {
$info=get_user_info($user);
if($info) {
return $info['id'];
} else {
return null;
}
}
function is_group_member($id,$user) {
$db = get_db();
$q = $db->prepare('SELECT id FROM groupmembers WHERE "group" = ? AND user = ?');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$q->bindValue(2, get_user_id($user), SQLITE3_INTEGER);
$r = $q->execute();
$ret = $r->fetchArray(SQLITE3_NUM);
return (bool) $ret;
}
function add_group_member($id,$user) {
$db = get_db();
$userid=get_user_id($user);
$q = $db->prepare('INSERT INTO groupmembers ("group", user) VALUES (?, ?)');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$q->bindValue(2, $userid, SQLITE3_INTEGER);
$ret = $q->execute();
$db->close();
if($ret) {
writelog("Added user $user to group " . get_group_name($id) . ".");
} else {
writelog("Failed to add user $user to group " . get_group_name($id) . ".");
}
return $ret;
}
?>

View file

@ -104,6 +104,26 @@ function get_all_users() {
return $ret;
}
/* Fetches a list of usernames from the DB for autocomplete.
* Restricts list by $term which can appear anywhere in the username
* Restricts results to $num responses
*/
function get_usernames_filtered($term, $num = 10) {
$db = get_db();
$q = $db->prepare("SELECT emailaddress FROM users WHERE emailaddress LIKE ? ORDER BY emailaddress LIMIT 0, ?");
$q->bindValue(1, "%" . $term . "%", SQLITE3_TEXT);
$q->bindValue(2, $num, SQLITE3_INTEGER);
$r = $q->execute();
$ret = array();
while ($row = $r->fetchArray(SQLITE3_NUM)) {
array_push($ret, $row[0]);
}
$db->close();
return $ret;
}
function get_user_info($u) {
$db = get_db();
$q = $db->prepare('SELECT * FROM users WHERE emailaddress = ?');