/**
  * Returns all users with permissions on an object, and their permissions
  * 
  * @param string $object_id object_id to check
  */
 public static function read_object_permissions($object_id, $master_only = false)
 {
     $error = new argent_error();
     $db = new argent_database();
     if (!argent_meta::object_registered($object_id)) {
         $error->add('1038', 'Object does not exist', $object_id, 'argent_uauth');
     }
     if ($error->has_errors()) {
         return $error;
     }
     $sql = "\r\n                    SELECT\r\n                        *\r\n                    FROM\r\n                        `ua_permissions`\r\n                    WHERE\r\n                        `object_id` = '{$db->escape_value($object_id)}'\r\n                    ";
     if ($master_only) {
         $sql .= "\r\n                    AND\r\n                        `master` = ''\r\n                    ";
     }
     $permissions = $db->returntable($sql);
     return $permissions;
 }
 public static function find_related($primary_object_id = null, $secondary_object_id = null, $relationship = null, $include_reverse = false)
 {
     $error = new argent_error();
     $db = new argent_database();
     if ($primary_object_id != null && !argent_meta::object_registered($primary_object_id)) {
         $error->add('1038', 'Object does not exist', $primary_object_id, 'argent_uauth');
     }
     if ($secondary_object_id != null && !argent_meta::object_registered($secondary_object_id)) {
         $error->add('1038', 'Object does not exist', $secondary_object_id, 'argent_uauth');
     }
     if (!is_string($relationship) && $relationship != null) {
         $error->add('1050', 'Invalid data type: expecting STRING', $relationship, 'argent_meta');
     }
     if ($error->has_errors()) {
         return $error;
     }
     $sql = "\r\n                    SELECT\r\n                        *\r\n                    FROM\r\n                        `ua_relationships`\r\n                    WHERE\r\n                        ";
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        (\r\n                        ";
     }
     if ($primary_object_id != null) {
         $sql .= "`primary_object_id` = '{$db->escape_value($primary_object_id)}'";
     }
     if ($primary_object_id != null && $secondary_object_id != null) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($secondary_object_id != null) {
         $sql .= "\r\n                        `secondary_object_id` = '{$db->escape_value($secondary_object_id)}'\r\n                      ";
     }
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        )\r\n                    ";
     }
     if ($include_reverse) {
         $sql .= "\r\n                        OR\r\n                          ";
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            (\r\n                            ";
         }
         if ($secondary_object_id != null) {
             $sql .= "`primary_object_id` = '{$db->escape_value($secondary_object_id)}'";
         }
         if ($primary_object_id != null && $secondary_object_id != null) {
             $sql .= "\r\n                        AND\r\n                        ";
         }
         if ($primary_object_id != null) {
             $sql .= "\r\n                            `secondary_object_id` = '{$db->escape_value($primary_object_id)}'\r\n                          ";
         }
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            )\r\n                        ";
         }
     }
     if ($relationship != null && ($primary_object_id != null || $secondary_object_id != null)) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($relationship != null) {
         $sql .= "\r\n                        `relationship` = '{$db->escape_value($relationship)}'\r\n                    ";
     }
     $relationship_data = $db->returntable($sql);
     return $relationship_data;
 }