2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2001-01-13 11:18:50 +01:00
|
|
|
* phpGroupWare API - Access Control List *
|
|
|
|
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
|
|
|
|
* Security scheme based on ACL design *
|
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall *
|
|
|
|
* -------------------------------------------------------------------------*
|
2001-01-16 14:52:32 +01:00
|
|
|
* This library is part of the phpGroupWare API *
|
|
|
|
* http://www.phpgroupware.org/api *
|
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-13 11:18:50 +01:00
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
2001-01-11 10:52:33 +01:00
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
class acl
|
|
|
|
{
|
2001-01-28 13:07:20 +01:00
|
|
|
var $db;
|
2001-01-20 00:45:37 +01:00
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
function acl()
|
|
|
|
{
|
|
|
|
global $phpgw;
|
|
|
|
$this->db = $phpgw->db;
|
|
|
|
}
|
2001-01-20 00:45:37 +01:00
|
|
|
|
2001-01-11 10:52:33 +01:00
|
|
|
/* This is a new class. These are sample table entries
|
|
|
|
insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_account_type, acl_rights)
|
|
|
|
values('filemanager', 'create', 1, 'u', 4);
|
|
|
|
insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_account_type, acl_rights)
|
|
|
|
values('filemanager', 'create', 1, 'g', 2);
|
|
|
|
insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_account_type, acl_rights)
|
|
|
|
values('filemanager', 'create', 2, 'u', 1);
|
|
|
|
insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_account_type, acl_rights)
|
|
|
|
values('filemanager', 'create', 2, 'g', 2);
|
|
|
|
*/
|
2001-01-20 00:45:37 +01:00
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
function get_rights($location,$appname = False, $user_id = ""){
|
|
|
|
global $phpgw, $phpgw_info;
|
2001-01-11 10:52:33 +01:00
|
|
|
if ($appname == False){
|
|
|
|
$appname = $phpgw_info["flags"]["currentapp"];
|
|
|
|
}
|
2001-01-28 13:07:20 +01:00
|
|
|
if($user_id == "") {
|
|
|
|
$user_id = $phpgw_info["user"]["account_id"];
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
// User piece
|
|
|
|
$sql = "select acl_rights from phpgw_acl where acl_appname='$appname'";
|
|
|
|
$sql .= " and (acl_location in ('$location','everywhere')) and ";
|
2001-01-28 13:07:20 +01:00
|
|
|
$sql .= "((acl_account_type = 'u' and acl_account = ".$user_id.")";
|
2001-01-11 10:52:33 +01:00
|
|
|
|
|
|
|
// Group piece
|
|
|
|
$sql .= " or (acl_account_type='g' and acl_account in (0"; // group 0 covers all users
|
2001-01-31 06:51:52 +01:00
|
|
|
$memberships = $phpgw->accounts->read_group_names($user_id);
|
2001-01-11 10:52:33 +01:00
|
|
|
if (is_array($memberships) && count($memberships) > 0){
|
|
|
|
for ($idx = 0; $idx < count($memberships); ++$idx){
|
|
|
|
$sql .= ",".$memberships[$idx][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sql .= ")))";
|
|
|
|
$rights = 0;
|
2001-01-20 00:45:37 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
if ($this->db->num_rows() == 0 && $phpgw_info["server"]["acl_default"] != "deny"){ return True; }
|
|
|
|
while ($this->db->next_record()) {
|
|
|
|
if ($this->db->f("acl_rights") == 0){ return False; }
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
2001-01-11 10:52:33 +01:00
|
|
|
}
|
2001-01-24 04:21:16 +01:00
|
|
|
return $rights;
|
|
|
|
}
|
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
function check($location, $required, $appname = False, $user_id = ""){
|
2001-01-27 05:07:29 +01:00
|
|
|
global $phpgw, $phpgw_info;
|
2001-01-28 13:07:20 +01:00
|
|
|
$rights = $this->get_rights($location,$appname, $user_id);
|
2001-01-24 04:21:16 +01:00
|
|
|
|
2001-01-11 10:52:33 +01:00
|
|
|
return !!($rights & $required);
|
|
|
|
}
|
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
function get_specific_rights($location, $appname = False, $id = "", $id_type = "u"){
|
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
|
|
|
|
if ($appname == False){
|
|
|
|
$appname = $phpgw_info["flags"]["currentapp"];
|
|
|
|
}
|
|
|
|
if($id == "") {
|
|
|
|
$id = $phpgw_info["user"]["account_id"];
|
|
|
|
}
|
|
|
|
// User piece
|
|
|
|
$sql = "select acl_rights from phpgw_acl where acl_appname='$appname'";
|
|
|
|
$sql .= " and acl_location = '$location' and ";
|
2001-01-30 03:12:03 +01:00
|
|
|
$sql .= "acl_account_type = '".$id_type."' and acl_account = ".$id;
|
2001-01-28 13:07:20 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
$rights = 0;
|
2001-01-30 11:11:34 +01:00
|
|
|
if ($this->db->num_rows() == 0 && $phpgw_info["server"]["acl_default"] != "deny"){
|
|
|
|
return True;
|
|
|
|
}
|
2001-01-28 13:07:20 +01:00
|
|
|
while ($this->db->next_record()) {
|
|
|
|
if ($this->db->f("acl_rights") == 0){ return False; }
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
|
|
|
}
|
|
|
|
return $rights;
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_specific($location, $required, $appname = False, $id = "", $id_type = "u"){
|
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
$rights = $this->get_specific_rights($location,$appname, $id, $id_type);
|
2001-02-02 13:28:39 +01:00
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
return !!($rights & $required);
|
|
|
|
}
|
|
|
|
|
2001-01-11 10:52:33 +01:00
|
|
|
function add($app, $location, $id, $id_type, $rights){
|
|
|
|
$sql = "insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_account_type, acl_rights)";
|
|
|
|
$sql .= " values('".$app."', '".$location."', ".$id.", '".$id_type."', ".$rights.")";
|
2001-01-20 00:45:37 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
2001-01-11 10:52:33 +01:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete($app, $location, $id, $id_type){
|
2001-01-30 03:12:03 +01:00
|
|
|
$sql = "delete from phpgw_acl where acl_appname like '".$app."'"
|
|
|
|
. " and acl_location like '".$location."' and "
|
|
|
|
. " acl_account_type = '".$id_type."' and acl_account = ".$id;
|
2001-01-20 00:45:37 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
2001-01-30 03:12:03 +01:00
|
|
|
return $this->db->num_rows();
|
2001-01-11 10:52:33 +01:00
|
|
|
}
|
|
|
|
|
2001-01-26 12:58:00 +01:00
|
|
|
function replace($app, $location, $id, $id_type, $rights){
|
|
|
|
$this->delete($app, $location, $id, $id_type);
|
|
|
|
$this->add($app, $location, $id, $id_type, $rights);
|
|
|
|
return True;
|
2001-01-11 10:52:33 +01:00
|
|
|
}
|
|
|
|
|
2001-01-28 13:07:20 +01:00
|
|
|
function get_app_list_for_id($location, $required, $id = "", $id_type = "u"){
|
2001-01-25 02:30:39 +01:00
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
if ($id == ""){ $id = $phpgw_info["user"]["account_id"]; }
|
|
|
|
$sql = "select acl_appname, acl_rights from phpgw_acl where acl_location = '$location' and ";
|
2001-01-28 02:37:34 +01:00
|
|
|
$sql .= "acl_account_type = '".$id_type."' and acl_account = ".$id;
|
2001-01-22 11:35:31 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
$rights = 0;
|
|
|
|
if ($this->db->num_rows() == 0 ){ return False; }
|
|
|
|
while ($this->db->next_record()) {
|
|
|
|
if ($this->db->f("acl_rights") == 0){ return False; }
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
|
|
|
if (!!($rights & $required) == True){
|
|
|
|
$apps[] = $this->db->f("acl_appname");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $apps;
|
|
|
|
}
|
|
|
|
|
2001-01-26 12:58:00 +01:00
|
|
|
function get_location_list($app, $required){
|
2001-01-22 11:35:31 +01:00
|
|
|
global $phpgw, $phpgw_info;
|
2001-01-26 12:58:00 +01:00
|
|
|
// User piece
|
|
|
|
$sql = "select acl_location, acl_rights from phpgw_acl where acl_appname = '$app' ";
|
|
|
|
$sql .= " and ((acl_account_type = 'u' and acl_account = '".$phpgw_info["user"]["account_id"]."')";
|
|
|
|
|
|
|
|
// Group piece
|
|
|
|
$sql .= " or (acl_account_type='g' and acl_account in (0"; // group 0 covers all users
|
2001-01-31 06:51:52 +01:00
|
|
|
$memberships = $phpgw->accounts->read_group_names($phpgw_info["user"]["account_id"]);
|
2001-01-26 12:58:00 +01:00
|
|
|
if (is_array($memberships) && count($memberships) > 0){
|
|
|
|
for ($idx = 0; $idx < count($memberships); ++$idx){
|
|
|
|
$sql .= ",".$memberships[$idx][0];
|
|
|
|
}
|
2001-01-22 11:35:31 +01:00
|
|
|
}
|
2001-01-26 12:58:00 +01:00
|
|
|
$sql .= ")))";
|
|
|
|
|
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
$rights = 0;
|
|
|
|
if ($this->db->num_rows() == 0 ){ return False; }
|
|
|
|
while ($this->db->next_record()) {
|
|
|
|
if ($this->db->f("acl_rights") == 0){ return False; }
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
|
|
|
if (!!($rights & $required) == True){
|
|
|
|
$locations[] = $this->db->f("acl_location");
|
|
|
|
}else{
|
|
|
|
return False;
|
|
|
|
}
|
2001-01-22 11:35:31 +01:00
|
|
|
}
|
2001-01-26 12:58:00 +01:00
|
|
|
return $locations;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_location_list_for_id($app, $required, $id_type = "", $id = ""){
|
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
if ($id == ""){ $id = $phpgw_info["user"]["account_id"]; }
|
|
|
|
if ($id_type == ""){ $id_type = "u"; }
|
|
|
|
$sql = "select acl_location, acl_rights from phpgw_acl where acl_appname = '$app' and ";
|
2001-01-28 02:37:34 +01:00
|
|
|
$sql .= "acl_account_type = '".$id_type."' and acl_account = ".$id;
|
2001-01-22 11:35:31 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
$rights = 0;
|
|
|
|
if ($this->db->num_rows() == 0 ){ return False; }
|
|
|
|
while ($this->db->next_record()) {
|
2001-01-31 06:51:52 +01:00
|
|
|
if ($this->db->f("acl_rights")) {
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
|
|
|
if (!!($rights & $required) == True){
|
|
|
|
$locations[] = $this->db->f("acl_location");
|
|
|
|
}
|
2001-01-22 11:35:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $locations;
|
|
|
|
}
|
2001-01-26 12:58:00 +01:00
|
|
|
|
2001-01-30 11:46:09 +01:00
|
|
|
function get_ids_for_location($location, $required, $app = False, $id_type = "u"){
|
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
if ($app == False){
|
|
|
|
$app = $phpgw_info["flags"]["currentapp"];
|
|
|
|
}
|
2001-02-01 05:02:01 +01:00
|
|
|
$sql = "select acl_account, acl_rights from phpgw_acl where acl_appname = '$app' and ";
|
|
|
|
$sql .= "acl_account_type = '".$id_type."' and acl_location = '".$location."'";
|
2001-01-30 11:46:09 +01:00
|
|
|
$this->db->query($sql ,__LINE__,__FILE__);
|
|
|
|
$rights = 0;
|
|
|
|
if ($this->db->num_rows() == 0 ){ return False; }
|
|
|
|
while ($this->db->next_record()) {
|
|
|
|
$rights |= $this->db->f("acl_rights");
|
|
|
|
if (!!($rights & $required) == True){
|
|
|
|
$accounts[] = $this->db->f("acl_account");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $accounts;
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
} //end of acl class
|
2001-01-30 03:12:03 +01:00
|
|
|
?>
|