Jump to content

Delete exception, still deletes


Manaus
 Share

Recommended Posts

Hello, I have a page with this code

if ($input->get->progetto && $input->get->item) {

	$prg = (int) $input->get->progetto;
	$itm = (int) $input->get->item;

	$theitem = $pages->get($itm);
	echo $theitem->createdUser == $user; // prints nothing

	if ($user->isSuperuser() || $theitem->createdUser == $user) {
		$theitem->delete();
	}	
	$projurl = $pages->get($prj)->url;
	$session->redirect($projurl);
}

I get this error: "Error:     Exception: This page may not be deleted (in /Users/utente/Sites/gammapw/wire/core/Pages.php line 847)"

Still, the removing action takes place.

Any suggestion?

Thanks!

Link to comment
Share on other sites

$theitem = $pages->get($itm);

That line is too broad. If given the right ID, it could load anything in the site, including any user account. Narrow it down by supplying a template or parent or something to make sure you are getting what you expect. i.e. 

$theitem = $pages->get("template=item, id=$itm"); 
if ($user->isSuperuser() || $theitem->createdUser == $user) {

When comparing page objects (which is what users are) you'll want to compare the 'id' property:

if($user->isSuperuser() || $theitem->createdUser->id == $user->id) {

Also, you aren't checking that the page you retrieve actually exists. I'd suggest adding this:

if(!$theitem->id) throw new WireException("That page doesn't exist"); 
I get this error: "Error:     Exception: This page may not be deleted (in /Users/utente/Sites/gammapw/wire/core/Pages.php line 847)"

Still, the removing action takes place.

That exception occurs when you try to delete a system page or a NullPage. When that exception occurs, it halts program execution immediately. If something still got deleted, it would have had to have happened before that exception occurred. It doesn't look like your code could generate that error, so I'd guess there's a hook into Pages::delete that is attempting to delete another page related to the first deletion. 

Also, before deleting the page, I suggest checking if it is deleteable from an access control perspective:

if(!$theitem->deleteable()) throw new WireException("That page is not deleteable"); 
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...