Suggested new helper functions: SEC_filterPermissions, SEC_hasAccess2 HEAD
authorDirk Haun <dirk@haun-online.de>
Sun Oct 04 19:56:54 2009 +0200 (4 months ago)
branchHEAD
changeset 7362aaa5a1f1850e
parent 7361cb9ad1e4b759
child 73638b1e9b058fbd
Suggested new helper functions: SEC_filterPermissions, SEC_hasAccess2
system/lib-security.php
     1.1 --- a/system/lib-security.php	Sun Oct 04 18:07:39 2009 +0200
     1.2 +++ b/system/lib-security.php	Sun Oct 04 19:56:54 2009 +0200
     1.3 @@ -1222,4 +1222,74 @@
     1.4      return $retval;
     1.5  }
     1.6  
     1.7 +/**
     1.8 +* Prepare an array of the standard permission values
     1.9 +*
    1.10 +* This helper functions does the following:
    1.11 +* 1) filter permission values, e.g. after a POST request
    1.12 +* 2) translates the permission checkbox arrays into numerical values
    1.13 +* 3) ensures that all the standard permission entries are set, so you don't
    1.14 +*    have to check with isset() all the time
    1.15 +*
    1.16 +* <code>
    1.17 +* $PERM = SEC_filterPermissions($_POST);
    1.18 +* if ($PERM['perm_anon'] != 0) { ...
    1.19 +* </code>
    1.20 +*
    1.21 +* @param    array   $A  array to filter on, e.g. $_POST
    1.22 +* @return   array       array of only the 6 standard permission values
    1.23 +* @see      SEC_getPermissionValues
    1.24 +*
    1.25 +*/
    1.26 +function SEC_filterPermissions($A)
    1.27 +{
    1.28 +    $retval = array();
    1.29 +
    1.30 +    if (isset($A['owner_id'])) {
    1.31 +        $retval['owner_id'] = COM_applyFilter($A['owner_id'], true);
    1.32 +    } else {
    1.33 +        $retval['owner_id'] = 0;
    1.34 +    }
    1.35 +
    1.36 +    if (isset($A['group_id'])) {
    1.37 +        $retval['group_id'] = COM_applyFilter($A['group_id'], true);
    1.38 +    } else {
    1.39 +        $retval['group_id'] = 0;
    1.40 +    }
    1.41 +
    1.42 +    $perms = array('perm_owner', 'perm_group', 'perm_members', 'perm_anon');
    1.43 +
    1.44 +    $B = array();
    1.45 +    foreach ($perms as $p) {
    1.46 +        if (isset($A[$p])) {
    1.47 +            $B[$p] = $A[$p];
    1.48 +        } else {
    1.49 +            $B[$p] = array();
    1.50 +        }
    1.51 +    }
    1.52 +
    1.53 +    $B = SEC_getPermissionValues($B['perm_owner'], $B['perm_group'],
    1.54 +                                 $B['perm_members'], $B['perm_anon']);
    1.55 +    for ($i = 0; $i < 4; $i++) {
    1.56 +        $retval[$perms[$i]] = $B[$i];
    1.57 +    }
    1.58 +
    1.59 +    return $retval;
    1.60 +}
    1.61 +
    1.62 +/**
    1.63 +* Helper function for when you want to call SEC_hasAccess and have all the
    1.64 +* values to check in an array.
    1.65 +*
    1.66 +* @param    array   $A  array with the standard permission values
    1.67 +* @return   int         returns 3 for read/edit 2 for read only 0 for no access
    1.68 +* @see      SEC_hasAccess
    1.69 +*
    1.70 +*/
    1.71 +function SEC_hasAccess2($A)
    1.72 +{
    1.73 +    return SEC_hasAccess($A['owner_id'], $A['group_id'], $A['perm_owner'],
    1.74 +                         $A['perm_group'], $A['perm_members'], $A['perm_anon']);
    1.75 +}
    1.76 +
    1.77  ?>