<?php
  /*

      acl_issue.inc

      ACL Management for issues.php

      ----
  
      Created by:
        Copyright (C) 2005  1stBnA GmbH 
        Copyright (C) 2005  Stephan Kaufhold (s.kauhold@1stbna.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program 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 General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */


/** Get user Acl for a issue.
 *  @param $issue Hash with all issue related options.
 *  @param $uid Userid.
 *  
 *  @return Hash with ACL.
 */

function get_issue_acl($issue,$uid)
{
  global $usertypesRS, $usertype;// Only for development.
  $dom = db_recordset("SELECT domain from tbl_Sites where id=".$issue['site']);
  
  $sql = "SELECT tbl_UserTypes.type AS usertypename FROM tbl_UserTypes,tbl_UserSites,tbl_Sites WHERE tbl_UserSites.userid=$uid AND tbl_UserSites.site=tbl_Sites.id AND tbl_Sites.domain={$dom[0]['domain']} AND tbl_UserTypes.id=tbl_UserSites.usertype ORDER BY tbl_UserTypes.sortorder DESC LIMIT 1";

  $usertRS = db_recordset($sql);

  if (($uid < 2) || 
    ($usertRS[0]['usertypename'] == '') // User has no relation within Domain
    )
  {
    // Conditions that never get access to a issue.
    $acl['denyall'] = true;
  }
  elseif ($usertRS[0]['usertypename'] == "Domain Administrator")
  {
    $usertype = "Domain Administrator";
    if (get_closed_status($_SESSION['_domain']) == $issue['status'])
    {
      if ($issue[published] == 0)
      {
        $acl['edit_publish'] = true;
        $acl['edit_status'] = true;
        $acl['edit_issue'] = true;
        $acl['reset_issue'] = true;
        $acl['edit_remark'] = true;
        $acl['add_remark'] = true;
      }
      else
      {
        $acl['unpublish_issue'] = true;
      }
      $acl['view_remarks'] = true;
    }
    else
    {
      $user_prefs = get_user_prefs($_SESSION['_id']);

      if ($user_prefs['wrk-timer'])
      {
        $acl['view_wrktimer'] = true;
        $acl['use_wrktimer'] = true;
      }
      $acl['edit_time'] = true;
      $acl['edit_site'] = true;
      $acl['edit_assignedto'] = true;
      $acl['edit_contactinfo'] = true;
      $acl['edit_category'] = true;
      $acl['edit_details'] = true;
      $acl['edit_summary'] = true;
      $acl['edit_description'] = true;
      $acl['edit_solution'] = true;
      $acl['edit_location'] = true;
      $acl['edit_priority'] = true;
      $acl['edit_status'] = true;
      $acl['edit_level'] = true;
      $acl['edit_issue'] = true;
      $acl['reset_issue'] = true;
      $acl['edit_remark'] = true;
      $acl['add_remark'] = true;
    }
    $acl['edit_style'] = true;
    $acl['view_createdby'] = true;
    $acl['view_reportedby'] = true;
    $acl['view_site'] = true;
    $acl['view_assignedto'] = true;
    $acl['view_contactinfo'] = true;
    $acl['view_location'] = true;
    $acl['view_category'] = true;
    $acl['view_details'] = true;
    $acl['view_summary'] = true;
    $acl['view_description'] = true;
    $acl['view_solution'] = true;
    $acl['view_time'] = true;
    $acl['view_priority'] = true;
    $acl['view_status'] = true;
    $acl['view_level'] = true;
    $acl['show_confidential'] = true;
  }
  else
  {
    $sql = "SELECT tbl_UserTypes.type AS usertypename FROM tbl_UserTypes LEFT JOIN tbl_UserSites ON tbl_UserSites.usertype = tbl_UserTypes.id  WHERE tbl_UserSites.userid=".$uid." AND tbl_UserSites.site=".$issue[site];

    $usertypesRS = db_recordset($sql);

    if (count ($usertypesRS) > 0) // Has relation to the side
    {
      if ($usertypesRS[0]['usertypename'] == "Client")
      {
        $usertype = "Client";
        if ($issue['createdby'] == $uid ||
          $issue['reportedby'] == $uid)
        {
          if (get_closed_status($_SESSION['_domain']) == $issue['status'] ||
            $issue['recalled'] == 1)
          {
            $acl['view_remarks'] = true;
          }
          else
          {
            $user_prefs = get_user_prefs($_SESSION['_id']);

            if ($user_prefs['wrk-timer'])
            {
              $acl['view_wrktimer'] = true;
            }
            $acl['edit_style'] = true;
            $acl['recall_issue'] = true;
            $acl['add_remark'] = true;
          }

          $acl['view_createdby'] = true;
          $acl['view_reportedby'] = true;
          $acl['view_site'] = true;
          $acl['view_contactinfo'] = true;
          $acl['view_location'] = true;
          $acl['view_category'] = true;
          $acl['view_details'] = true;
          $acl['view_summary'] = true;
          $acl['view_description'] = true;
          $acl['view_solution'] = true;
          $acl['view_time'] = true;
          $acl['view_priority'] = true;
          $acl['view_status'] = true;
          $acl['view_level'] = true;
          $acl['view_assignedto'] = true;
        }
        else
        {
          if ($issue['published'] == 1 || $issue['recalled'] == 1)
          {
            $user_prefs = get_user_prefs($_SESSION['_id']);
            
            if ($user_prefs['pub-hide-su'] != 'true')
            {
              $acl['view_createdby'] = true;
              $acl['view_reportedby'] = true;
              $acl['view_site'] = true;
              $acl['view_assignedto'] = true;
              $acl['view_contactinfo'] = true;
              $acl['view_location'] = true;
              $acl['view_time'] = true;
              $acl['view_priority'] = true;
              $acl['view_status'] = true;
              $acl['view_level'] = true;
            }
            if ($user_prefs['pub-hide-remarks'] != 'true')
            {
              $acl['view_remarks'] = true;
            }
            $acl['view_category'] = true;
            $acl['view_details'] = true;
            $acl['view_summary'] = true;
            $acl['view_description'] = true;
            $acl['view_solution'] = true;
          }
          else
          {
            $acl['denyall'] = true;
          }
        }
      }
      elseif ($usertypesRS[0]['usertypename'] == "Support Agent")
      {
        $usertype = "Support Agent";

        $acl['view_createdby'] = true;
        $acl['view_reportedby'] = true;
        $acl['view_site'] = true;
        $acl['view_assignedto'] = true;
        $acl['view_contactinfo'] = true;
        $acl['view_location'] = true;
        $acl['view_category'] = true;
        $acl['view_details'] = true;
        $acl['view_summary'] = true;
        $acl['view_solution'] = true;
        $acl['view_description'] = true;
        $acl['view_time'] = true;
        $acl['view_priority'] = true;
        $acl['view_status'] = true;
        $acl['view_level'] = true;
        $acl['show_confidential'] = true;

        if ($issue['assignedto'] == $uid ||
          $issue['createdby'] == $uid ||
          $issue['reportedby'] == $uid)
        {
          if (get_closed_status($_SESSION['_domain']) == $issue['status'])
          {
            $acl['view_remarks'] = true;
          }
          else
          {
            $user_prefs = get_user_prefs($_SESSION['_id']);

            if ($user_prefs['wrk-timer'])
            {
              $acl['view_wrktimer'] = true;
              $acl['use_wrktimer'] = true;
            }
            $acl['edit_time'] = true;
            $acl['edit_assignedto'] = true;
            $acl['edit_contactinfo'] = true;
            $acl['edit_category'] = true;
            $acl['edit_details'] = true;
            $acl['edit_summary'] = true;
            $acl['edit_description'] = true;
            $acl['edit_location'] = true;
            $acl['edit_solution'] = true;
            $acl['edit_priority'] = true;
            $acl['edit_status'] = true;
            $acl['edit_level'] = true;
            $acl['edit_style'] = true;
            $acl['edit_issue'] = true;
            $acl['reset_issue'] = true;
            $acl['add_remark'] = true;
          }
        }
        elseif ($issue[assignedto] == 0) // If issue is not assigned.
        {
          $acl['edit_style'] = true;
          $acl['edit_issue'] = true;
          $acl['reset_issue'] = true;
          $acl['edit_assignedto'] = true;
        }
      }
      elseif ($usertypesRS[0]['usertypename'] == "Administrator")
      {
        $usertype = "Administrator";
        if (get_closed_status($_SESSION['_domain']) == $issue['status'])
        {
          if ($issue[published] == 0)
          {
            $acl['edit_status'] = true;
            $acl['edit_issue'] = true;
            $acl['reset_issue'] = true;
            $acl['edit_publish'] = true;
            $acl['edit_remark'] = true;
            $acl['add_remark'] = true;
          }
          else
          {
            $acl['unpublish_issue'] = true;
          }
          $acl['view_remarks'] = true;
        }
        else
        {
          $user_prefs = get_user_prefs($_SESSION['_id']);

          if ($user_prefs['wrk-timer'])
          {
            $acl['view_wrktimer'] = true;
            $acl['use_wrktimer'] = true;
          }
          $acl['edit_site'] = true;
          $acl['edit_contactinfo'] = true;
          $acl['edit_location'] = true;
          $acl['edit_category'] = true;
          $acl['edit_details'] = true;
          $acl['edit_summary'] = true;
          $acl['edit_description'] = true;
          $acl['edit_solution'] = true;
          $acl['edit_issue'] = true;
          $acl['reset_issue'] = true;
          $acl['edit_time'] = true;
          $acl['edit_priority'] = true;
          $acl['edit_status'] = true;
          $acl['edit_level'] = true;
          $acl['edit_assignedto'] = true;
          $acl['edit_remark'] = true;
          $acl['add_remark'] = true;
        }

        $acl['edit_style'] = true;
        $acl['view_createdby'] = true;
        $acl['view_reportedby'] = true;
        $acl['view_site'] = true;
        $acl['view_assignedto'] = true;
        $acl['view_contactinfo'] = true;
        $acl['view_location'] = true;
        $acl['view_category'] = true;
        $acl['view_details'] = true;
        $acl['view_summary'] = true;
        $acl['view_solution'] = true;
        $acl['view_description'] = true;
        $acl['view_time'] = true;
        $acl['view_priority'] = true;
        $acl['view_status'] = true;
        $acl['view_level'] = true;
        $acl['show_confidential'] = true;
      }
      elseif ($usertypesRS[0]['usertypename'] == "Site Contact")
      {
        $usertype = "Site Contact";
        if (get_closed_status($_SESSION['_domain']) == $issue['status'] || 
           $issue['recalled'] == 1)
        {
          $acl['view_remarks'] = true;
        }
        else
        {
          $user_prefs = get_user_prefs($_SESSION['_id']);

          if ($user_prefs['wrk-timer'])
          {
            $acl['view_wrktimer'] = true;
          }
          $acl['edit_style'] = true;
          $acl['recall_issue'] = true;
          $acl['add_remark'] = true;
        }

        $acl['view_createdby'] = true;
        $acl['view_reportedby'] = true;
        $acl['view_site'] = true;
        $acl['view_contactinfo'] = true;
        $acl['view_location'] = true;
        $acl['view_category'] = true;
        $acl['view_details'] = true;
        $acl['view_summary'] = true;
        $acl['view_description'] = true;
        $acl['view_solution'] = true;
        $acl['view_time'] = true;
        $acl['view_priority'] = true;
        $acl['view_status'] = true;
        $acl['view_level'] = true;
        $acl['view_assignedto'] = true;
      }
    }
    else
    {
      if ($usertRS[0]['usertypename'] != '')
      {
        // All others users within the domain.
        if ($issue['published'] == 1)
        {
          // Knowledge Database is accessible to all in same Domain
          $user_prefs = get_user_prefs($_SESSION['_id']);
          
          if ($user_prefs['pub-hide-su'] != 'true')
          {
            $acl['view_createdby'] = true;
            $acl['view_reportedby'] = true;
            $acl['view_site'] = true;
            $acl['view_assignedto'] = true;
            $acl['view_contactinfo'] = true;
            $acl['view_location'] = true;
            $acl['view_time'] = true;
            $acl['view_priority'] = true;
            $acl['view_status'] = true;
            $acl['view_level'] = true;
          }
          if ($user_prefs['pub-hide-remarks'] != 'true')
          {
            $acl['view_remarks'] = true;
          }
          $acl['view_category'] = true;
          $acl['view_details'] = true;
          $acl['view_summary'] = true;
          $acl['view_description'] = true;
          $acl['view_solution'] = true;
        }
        else
        {
          // Deny if not published.
          $acl['denyall'] = true;
        }
      }
    }  
  }
  return $acl;
}
?>
