I've been reading CakePHP's 2.0 migration guide where it's stated that cakeError() has been removed because it was used for exceptions. It's a really weird change IMHO because I used it to block access to unauthorized users or to trigger an error when the paginated items exceeded the total, and things like that.
And now what? Should I just throw a die() or a redirect? I really want to let know the users that something was not found and Cake used to provie a stright way to do so... now it doesn't.
Any thoughts/hacks/workarounds about it? Thanks, happy holidays!
You have to throw the corresponding exception, in your case the NotFoundException:
throw new NotFoundException();
See also the chapter about exceptions in the cook book.
try this
if ($this->Session->read('Auth.User.role') == 'P' || $this->Session->read('Auth.User.role') == 'U') {
//die('you are not allowed to access this page');
//throw new ForbiddenException;
throw new NotFoundException('404 Error - Page not found');
}
Related
I've started creating a discord bot and I've wanted it to start moderating my server. my first command to try was 'mass delete' or 'purge' which deletes the number of messages a user gives.
But I'm pretty new to java and discord.js so I have no idea how to start.
// code here to check if an mod or above
// code to delete a number of message a user gave
}```
For permissions checking, you could simply check for the message author's roles using:
if (!message.member.roles.cache.has(moderator role object)) return;
Overall, you should be looking at the bulkDelete() function that can be found here.
Although, since you don't really know how to really develop bots yet from what you're saying, I won't be telling you the answer to your problem straight ahead. I'd highly recommend learning some JavaScript basics (Also, keep in mind Java != JavaScript) as well as Discord.js basics before you jump head straight into what you want to create.
I'd highly recommend WornOffKeys on YouTube, they're fantastic.
Check user permission first using
if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.channel.send("You have no permission to access this command")
check bot permission using
if (!message.guild.me.permissions.has("MANAGE_MESSAGES")) return message.channel.send("This bot have no permission to delete msg!")
return a message when user havnt mention a select message
if (!args[0]) return message.channel.send("no messages have been selected")
then as Bqre said using bulkdelete()
try {
await message.channel.bulkDelete(args[0])
} catch (e) {
console.log(e); //err
}
hope you find it useful!
Let's say I have User model and Post model. Post model contains field user_id.
User has $hasMany on Post and Post has $belongsTo on User.
I have some post edit action:
PostsController::edit($id) {
if($this->request->isPost())
{
$this->Post->id = $id;
$this->Post->save();
}
$post = $this->Post->read($id);
$this->set(compact('post'));
}
I use AuthComponent to login users.
How can I prevent user from editing some1 else post? Is there any cake build in function/option to do this? Because some1 can login and post edit action with any id. It's not even case of saving the post data - let's say post is private (only owner should see it) - when someone will call posts/edit/some_id it will see the edit form of this post...
The easier way is to just add this at the beginning of edit action:
$this->Post->id = $id;
if($this->Post->readField('user_id') != $this->Auth->user('id'))
{ //trigger error or redirect }
But I would have to add this at the beginning of each action that updates/reads any data that belongs to some user. So I am looking for more elegant way to do this.
Well an exact example ( handily using a Post/User model too ) is available in the cake manual
Everyones a winner!
Well there is no way of avoiding adding a line to check if a user is authorized to perform an action. Even if you use ACL (Access Control Lists), which is one of Cake's most powerful features.
But as you speak of elegance in general, ACLs will be beauty at its best :) Careful though, they've got a steep learning curve. Don't give up easy, its well worth it.
You should see ACLs from the Book http://book.cakephp.org/1.3/view/1543/Simple-Acl-controlled-Application
I would like to control and embed my send() calls in a try/except, but I'm not sure of what errors it can produce.
Looking around the SDK, it seems like MailServiceError is the one, but not sure since I don't know how to test an error like that.
Can anyone confirm this?
Here are the exceptions that can be thrown by a call to send(): https://developers.google.com/appengine/docs/python/mail/exceptions
Here's an example of how you could catch these:
from google3.apphosting.api import mail
# other code to create 'message' here
try:
message.send()
except mail.InvalidSender, e:
# Do something special for an invalid sender error
pass
except mail.Error, e:
# This will catch all the other exceptions in the doc above.
pass
CakePHP cannot get multiple emails send after a previous execution..
this is an example:
$Email = new CakeEmail('smtp');
$Email->emailFormat('html')
->template('myTemplate')
->to('someEmail#email.com')
->subject('someSubject')
->send('MyMessage');
unset($Email);
re-Instance cakeEmail
$arrEmails = array('email1#email.com','email2#email.com','email3#email.com','email4#email.com','email5#email.com');
$Email = new CakeEmail('smtp');
$Email->emailFormat('html')
->template('mySecondTemplate')
->to($arrEmails)
->subject('OtherSubject')
->send('MyOtherMessage');
I've also tried to create 2 different instances of cakeEmail but the same occur.
Else I've tried is to put in a loop "for" but nothing happens.
If anyone has an idea of what is happening here I would be very helpful and I appreciate it.
Thank you.
Take a look at the reset() method:
http://api20.cakephp.org/class/cake-email#method-CakeEmailreset
I am developing an application for Windows Phone 7. I am trying to use services which are provided by the web site I am trying to get information from. I am using an asynchronous request. So if I try to get information from a web site without any authentication I use this code:
EventSrv.EventSrvSoapClient client = new EventSrv.EventSrvSoapClient();
client.GetAppointmentsAsync();
client.GetAppointmentsCompleted += new EventHandler<EventSrv.GetAppointmentsCompletedEventArgs>(events_completed);
and it works fine. But as soon as I want to use a service from a web site which requires authentication I get a
CommunicationException: _innerException:"Server returned an error: Not Found"
at
public L2P.DocumentsService.GetDocumentsResponse EndGetDocuments(System.IAsyncResult result)
{
object[] _args = new object[0];
//Between this line
L2P.DocumentsService.GetDocumentsResponse _result = ((L2P.DocumentsService.GetDocumentsResponse)(base.EndInvoke("GetDocuments", _args, result)));
//and this line
return _result;
}
I am passing the credentials the following way:
DocumentsService.BaseServiceSoapClient docClient = new DocumentsService.BaseServiceSoapClient();
docClient.ClientCredentials.UserName.UserName = Variables.username;
docClient.ClientCredentials.UserName.Password = Variables.password;
docClient.GetDocumentsCompleted += new EventHandler<DocumentsService.GetDocumentsCompletedEventArgs>(getDocumentsCompleted);
docClient.GetDocumentsAsync();
It actually doesn't matter if I pass the credentials or not, I get the same exception. I don't really know what the problem is, maybe it has nothing to do with the authentication. I've read all the articles here on CommunicationException but they couldn't solve my problem.
Any help will be appreciated!
I've finally figured it out! The server uses Basic Authentication and the header is set to "POST" by default. So I needed to modify the Header, set it to "Basic" and add the credentials as well. Furthermore the
CommunicationException: "Server returned an error: Not Found"
always appear if there is any unhandled exception. So you need to debug and check the _innerException for more information.