Пример #1
0
 /**
  * Save Business Unit
  *
  * @return void
  * @author
  **/
 function save_business_unit()
 {
     $out = array();
     if (!$_POST) {
         $out['error'] = 'No data';
     } elseif (isset($_POST['unitid'])) {
         $business_unit = new Business_unit();
         // Translate groups to single entries
         $translate = array('keys' => 'key', 'machine_groups' => 'machine_group', 'users' => 'user', 'managers' => 'manager');
         $unitid = $_POST['unitid'];
         // Check if new unit
         if ($unitid == 'new') {
             $unitid = $business_unit->get_max_unitid() + 1;
         }
         $out['unitid'] = $unitid;
         // Check if there are changed items
         if (isset($_POST['iteminfo'])) {
             $groups = array();
             // If sent a '#', no items are in the iteminfo array
             // proceed with empty groups array
             if (!in_array('#', $_POST['iteminfo'])) {
                 // Loop through iteminfo
                 foreach ($_POST['iteminfo'] as $entry) {
                     // No key, create new
                     if ($entry['key'] === '') {
                         $mg = new Machine_group();
                         $newgroup = $mg->get_max_groupid() + 1;
                         // Store name
                         $mg->merge(array('id' => '', 'groupid' => $newgroup, 'property' => 'name', 'value' => $entry['name']));
                         $mg->save();
                         // Store GUID key
                         $mg->merge(array('id' => '', 'groupid' => $newgroup, 'property' => 'key', 'value' => get_guid()));
                         $mg->save();
                         $groups[] = $newgroup;
                     } else {
                         // Add key to list
                         $groups[] = intval($entry['key']);
                     }
                 }
             }
             // Set new machine_groups to list
             $_POST['machine_groups'] = $groups;
             unset($_POST['iteminfo']);
         }
         foreach ($_POST as $property => $val) {
             // Skip unitid
             if ($property == 'unitid') {
                 continue;
             }
             if (is_scalar($val)) {
                 $business_unit->id = '';
                 $business_unit->retrieve_one('unitid=? AND property=?', array($unitid, $property));
                 $business_unit->unitid = $unitid;
                 $business_unit->property = $property;
                 $business_unit->value = $val;
                 $business_unit->save();
                 $out[$property] = $val;
             } else {
                 // Check if this is a valid property
                 if (!isset($translate[$property])) {
                     $out['error'][] = 'Illegal property: ' . $property;
                     continue;
                 }
                 // Translate property to db entry
                 $name = $translate[$property];
                 $business_unit->delete_where('unitid=? AND property=?', array($unitid, $name));
                 foreach ($val as $entry) {
                     // Empty array placeholder
                     if ($entry === '#') {
                         $out[$property] = array();
                         continue;
                     }
                     $business_unit->id = '';
                     $business_unit->unitid = $unitid;
                     $business_unit->property = $name;
                     $business_unit->value = is_numeric($entry) ? 0 + $entry : $entry;
                     $business_unit->save();
                     $out[$property][] = is_numeric($entry) ? 0 + $entry : $entry;
                 }
             }
         }
     } else {
         $out['error'] = 'Unitid missing';
     }
     $obj = new View();
     $obj->view('json', array('msg' => $out));
 }
Пример #2
0
 /**
  * Set session properties
  *
  **/
 function set_session_props($show = false)
 {
     // Initialize session
     $this->authorized();
     // Check if we are in a session
     if (!isset($_SESSION['auth'])) {
         $msg = array('error' => 'unauthorized');
         $obj = new View();
         $obj->view('json', array('msg' => $msg));
         return;
     }
     // Default role is user
     $_SESSION['role'] = 'user';
     $_SESSION['role_why'] = 'Default role';
     // Find role in config for current user
     foreach (conf('roles', array()) as $role => $members) {
         // Check for wildcard
         if (in_array('*', $members)) {
             $_SESSION['role'] = $role;
             $_SESSION['role_why'] = 'Matched on wildcard (*) in ' . $role;
             break;
         }
         // Check if user or group is present in members
         foreach ($members as $member) {
             if (strpos($member, '@') === 0) {
                 // groups (start with @)
                 if (in_array(substr($member, 1), $_SESSION['groups'])) {
                     $_SESSION['role'] = $role;
                     $_SESSION['role_why'] = 'member of ' . $member;
                     break 2;
                 }
             } else {
                 // user
                 if ($member == $_SESSION['user']) {
                     $_SESSION['role'] = $role;
                     $_SESSION['role_why'] = $member . ' in "' . $role . '" role array';
                     break 2;
                 }
             }
         }
     }
     // Check if Business Units are enabled in the config file
     $bu_enabled = conf('enable_business_units', FALSE);
     // Check if user is global admin
     if ($_SESSION['auth'] == 'noauth' or $_SESSION['role'] == 'admin') {
         unset($_SESSION['business_unit']);
     } elseif (!$bu_enabled) {
         // Regular user w/o business units enabled
         unset($_SESSION['business_unit']);
     } elseif ($bu_enabled) {
         // Authorized user, not in business unit
         $_SESSION['role'] = 'nobody';
         $_SESSION['role_why'] = 'Default role for Business Units';
         $_SESSION['business_unit'] = 0;
         // Lookup user in business units
         $bu = new Business_unit();
         if ($bu->retrieve_one("property IN ('manager', 'user') AND value=?", $_SESSION['user'])) {
             $_SESSION['role'] = $bu->property;
             // manager, user
             $_SESSION['role_why'] = $_SESSION['user'] . ' found in Business Unit ' . $bu->unitid;
             $_SESSION['business_unit'] = $bu->unitid;
         } else {
             // Lookup groups in Business Units
             foreach ($_SESSION['groups'] as $group) {
                 if ($bu->retrieve_one("property IN ('manager', 'user') AND value=?", '@' . $group)) {
                     $_SESSION['role'] = $bu->property;
                     // manager, user
                     $_SESSION['role_why'] = 'Group "' . $group . '" found in Business Unit ' . $bu->unitid;
                     $_SESSION['business_unit'] = $bu->unitid;
                     break;
                 }
             }
         }
     }
     // Set machine_groups
     if ($_SESSION['role'] == 'admin' or !$bu_enabled) {
         // Can access all defined groups (from machine_group)
         // and used groups (from reportdata)
         $mg = new Machine_group();
         $report = new Reportdata_model();
         $_SESSION['machine_groups'] = array_unique(array_merge($report->get_groups(), $mg->get_group_ids()));
     } else {
         // Only get machine_groups for business unit
         $_SESSION['machine_groups'] = $bu->get_machine_groups($bu->unitid);
     }
     // Show current session info
     if ($show) {
         $obj = new View();
         $obj->view('json', array('msg' => $_SESSION));
     }
 }