Welcome Guest, Not a member yet? Register   Sign In
Help with PHP logic needed
#1

[eluser]mdcode[/eluser]
I have a form that I need to restrict certain entries on. The users of the system have two access "codes", one for the state the user is located, and one for department they are in. I need to create some rule logic where I do not have to repeat the form code in the file but check conditions on each of the user settings. I also have a 'catch all' code of 0.

To try to make it clearer, this is the code as it stands:

Code:
if ($this->session->userdata('state') == $project->state_code || 0 && $this->session->userdata('dept') == $project->dept || 0) {

// Display the form
...

} else { echo "You do not have access!"; }

The conditions for being able to edit the project are:
The users state matches that of the project attempting to be edited
AND
The users department matches that of the project attempting to be edited
OR
Either the users state or department equals 0.

I appreciate any help on this as reading up elsewhere is not yielding any definitive answers.
#2

[eluser]Thorpe Obazee[/eluser]
Code:
if ((($this->session->userdata('state') == $project->state_code) OR  ($this->session->userdata('state') == 0)) AND (($this->session->userdata('dept') == $project->dept) OR ($this->session->userdata('dept') == $project->dept)))
#3

[eluser]mdcode[/eluser]
Thanks for the reply bargainph, however although this works great for a normal user, it's made the catch-all code 0 useless, and as such the admin cannot edit anything. Any ideas what's wrong as your code seems fine.

EDIT:
Nevermind... spoke too soon - you forgot the last 0 instead of
Code:
$this->session->userdata('user_dept') == $project->dept

should have been
Code:
$this->session->userdata('user_dept') == 0
#4

[eluser]wiredesignz[/eluser]
Don't call the userdata() method 4 times, try calling only once each and assign variables.
Code:
if (( ! $state = $this->session->userdata('state') AND ! $dept = $this->session->userdata('dept')) OR ($state == $project->state AND $dept == $project->dept))
EDIT:
Altered above to suit the logic you asked for.
#5

[eluser]mdcode[/eluser]
Thanks for that wiredesignz - although I think I see what you have put, I don't get how the logic works, could you possibly run me through it in English please? Thanks


PS. I haven't tested this as working, as I don't know how it works.
#6

[eluser]wiredesignz[/eluser]
Basically it assigns session('state') to $state, session('dept') to $dept and tests both for ZERO if that fails it tests $state and $dept against the $project object variables.

EDIT:
The logic there may still need tweaking to get the testing order correct.
#7

[eluser]mdcode[/eluser]
Thanks for explaining it but it doesn't work... I had to modify it slightly otherwise I just got a parse error from it, to this:
Code:
if (( ! $home_state = $this->session->userdata('home_state') OR $home_state = $project->state AND ! $home_dept = $this->session->userdata('home_dept') OR $home_dept = $project->dept))
Looking at it, it seems fine, however the access is not locked down as I have a user that shouldn't be able to edit all departments projects (hence why I need the 0 access), but can...
#8

[eluser]wiredesignz[/eluser]
The parse error was only a missing parentheses. I also had another two errors in my code above.

You need to do a comparison of $home_dept to $project->dept not an assignment. Alter the = to == for that and the home_state comparison.
#9

[eluser]mdcode[/eluser]
I'm sorry, I'm having a really hard time understanding. Whatever I am trying, it just doesn't work, either I get an undefined variable message, or the form doesn't display, or the "security" is set incorrectly.

What I am understanding from your code, is if home_state is not set, set it to the home_state of the user or to the state of the project, and the same goes for the home_dept. This seems wrong to me. Any further explanation would be helpful and appreciated. Thanks.
#10

[eluser]Thorpe Obazee[/eluser]
puzzle:
Code:
if (( ! $state = $this->session->userdata('state') AND ! $dept = $this->session->userdata('dept')) OR ($state == $project->state AND $dept == $project->dept))
explanation:
Code:
if (($state is FALSE/0 AND $dept is FALSE/0) OR ($state == $project->state AND $dept == $project->dept))

make sense?

EDIT: Take note that assignment takes place in the first part. You only get to the second part when the first part is FALSE.




Theme © iAndrew 2016 - Forum software by © MyBB