Jump to content

Recreate UNIX file permissions


Andy-H

Recommended Posts

Hi, I am trying to recreate the UNIX file permissions to test weather today is in a list of days of the week in which a trigger should be active,

 

 

I have a table storing an integer value for the days active and am comparing that with the weekday number, however the results are not as I expect, where am I going wrong?

 

 



// days_active = (32+64) = 96 = Saturday & Sunday || ( 6 & 7)
SELECT * FROM `triggers` WHERE IF(DATE_FORMAT(NOW(), '%w')=0,7,DATE_FORMAT(NOW(), '%u')) & days_active

Link to comment
Share on other sites

I think I've got it worked out, I just need to convert

1 to 1
2 to 2
3 to 4
4 to 8
5 to 16
6 to 32
7 to 64

So I can use a bitwise comparison and check for say monday to friday the number to check against would be 1 + 2 + 4 + 8 + 16 = 31

 

 

So

1  & 31  = true
2  & 31  = true
4  & 31  = true
8  & 31  = true
16 & 31 = true
32 & 31 = false
64 & 31 = false

 

 

Is there a function to make this conversion in MySQL?

Link to comment
Share on other sites

Sorry, as you may have noticed I'm finding it hard to explain what I am trying to do, perhaps this will help,

 

 


$test = array(1=>1, 2=>2, 3=>4, 4=>8, 5=>16, 6=>32, 7=>64);
for( $i = 1; $i <= 7; $i++)
{
   echo ($test[$i] & 13) . '<br >'; // 13 = 1 + 4 + 8 ( Mon, Wed, Thu)
}

 

 

Outputs:

 

 

1 // true
0 // false
4 // true
8 // true
0 // false
0 // false
0 // false

[/size]So now all I need is a better way to convert 1 to1, 2 to 2, 3 to 4, 5 to 8 etc. than a load of IF() statements. Any idea?

Link to comment
Share on other sites

Ok I'm home now and can explain what I was trying to do, basically, given a set of weekdays on which a data rule should be in place, and a date to compare against them, I needed to extract the weekday and determine if a datarule was active.

 

 

I read a reply a while ago on here that explained how unix file permissions worked, and thought the implementation would be perfect for this scenario.

 

 

So to do it I wrote this script:

 

 

function pow2(&$value)
{
$value = pow(2, $value); // set $value to 2 to the exponent (power) of $value
}
if ( !empty($_POST['days']) )
{
array_map($_POST['days'], 'pow2');
$checkdays = array_sum($_POST['days']);
echo '<pre>'. print_r($_POST['days'],1) .'</pre>'. $checkdays;
}

 

 

<form action="" method="post">
<input type="checkbox" name="days[]" value="0" >Mon <br >
<input type="checkbox" name="days[]" value="1" >Tue <br >
<input type="checkbox" name="days[]" value="2" >Wed <br >
<input type="checkbox" name="days[]" value="3" >Thu <br >
<input type="checkbox" name="days[]" value="4" >Fri <br >
<input type="checkbox" name="days[]" value="5" >Sat <br >
<input type="checkbox" name="days[]" value="6" >Sun <br >
<input type="submit" >
</form>

 

 

So if you were to check Mon, Wed and Thu you would get

 

 

Mon ( 2 ^ 0 ) = 1
Wed ( 2 ^ 2 ) = 4
Thu ( 2 ^ 3 ) = 8
              = 13

So I would set rule_checkdays to 13, now given a date I need to work out the day and use that as the exponent on 2.

(I will use NOW() to replace my date field for this example)

So:

IF(DATE_FORMAT(NOW(), '%w')=0,7,DATE_FORMAT(NOW(), '%w')) - 1       // Mon = 0, Tue = 1, Wed = 2, Thu = 3, Fri = 4, Sat = 5, Sun = 6
POW(2, IF(DATE_FORMAT(NOW(), '%w')=0,7,DATE_FORMAT(NOW(), '%w'))-1) // Mon = 1, Tue = 2, Wed = 4, Thu = 8, Fri = 16, Sat = 32, Sun = 64

So today is Thu so the above would return 8, which represented by binary is:

 

 

00001000

And 13 is

00001101

 

 

So with a bitwise &

 

 

00001101 (13)
&
00001000 ( 
=
00001000 ( (bool[true])

 

 

Now if today were Fri ( 16 ) that would equate to:

00001101 (13)
&
00010000 (16)
=
00000000 (0)(bool[false])

 

 

Or Tue ( 2 )

00001101 (13)
&
00000010 ( 2)
=
00000000 ( 0)(bool[false])

 

 

So now,

POW(2, IF(DATE_FORMAT(NOW(), '%w')=0,7,DATE_FORMAT(NOW(), '%w'))-1) & days_active

 

 

Will result to true if a rule is active today, false if not. Simple as that lol

Link to comment
Share on other sites

 

Read    = 1

Write    = 2

Ecexcute = 4

 

 

Owner = Read & Write & Execute ( 1 + 2 + 4 ) (7)

Group = Write & Execute        ( 1 + 4    ) (5)

All  = Read                  ( 1        ) (1)

 

 

then something like

7 = 0111
3 = 0011
1 = 0001

 

 

//owner [7]
if ( $action == 'r' )
{
   if ( $premissions & 1 )
   {
      // 7 = 00000111
      &
      // 1 = 00000001
             00000001 = 1 (BOOL[true])
      //allow
   }
}
elseif ( $action == 'w' )
{
   if ( $premissions & 2 )
   {
      // 7 = 00000111
      &
      // 2 = 00000010
             00000010 = 2 (BOOL[true])
      //allow
   }
}
elseif ( $action == 'x' )
{
   if ( $premissions & 4 )
   {
      // 7 = 00000111
      &
      // 4 = 00000100
             00000100 = 4 (BOOL[true])
      //allow
   }
}

//group [5]
if ( $action == 'r' )
{
   if ( $premissions & 1 )
   {
      // 5 = 00000101
      &
      // 1 = 00000001
             00000001 = 1 (BOOL[true])
      //allow
   }
}
elseif ( $action == 'w' )
{
   if ( $premissions & 2 )
   {
      // 5 = 00000101
      &
      // 2 = 00000010
             00000000 = 0 (BOOL[false])
      //deny
   }
}
elseif ( $action == 'x' )
{
   if ( $premissions & 4 )
   {
      // 5 = 00000101
      &
      // 4 = 00000100
             00000100 = 4 (BOOL[true])
      //allow
   }
}

//all [1]
if ( $action == 'r' )
{
   if ( $premissions & 1 )
   {
      // 1 = 00000001
      &
      // 1 = 00000001
             00000001 = 1 (BOOL[true])
      //allow
   }
}
elseif ( $action == 'w' )
{
   if ( $premissions & 2 )
   {
      // 1 = 00000001
      &
      // 2 = 00000010
             00000000 = 0 (BOOL[false])
      //deny
   }
}
elseif ( $action == 'x' )
{
   if ( $premissions & 4 )
   {
      // 1 = 00000001
      &
      // 4 = 00000100
             00000000 = 0 (BOOL[false])
      //deny
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.