• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Noob Plugin question: Alterin error message on filter hook.

Scheduled Pinned Locked Moved Solved NodeBB Development
18 Posts 5 Posters 6.0k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #2

    Instead of returning an error, try changing the read property that is passed to the hook to false, this way users will get a you don't have permission error message.

    jareyJ 1 Reply Last reply
    1
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to <baris> on last edited by jarey
    #3

    @baris said in Noob Plugin question: Alterin error message on filter hook.:

    Instead of returning an error, try changing the read property that is passed to the hook to false, this way users will get a you don't have permission error message.

    I'm already doing that, in addition of firing the error, like this:

    # code block
    postContent.read = false;
    callback(new Error(tagsTitle.mostrarError[0]+tagsTitle.mensajeError[i]+tagsTitle.mostrarError[1]), postContent);
    

    It is true, what you are telling, that if i make the callback without the error, the resulting message showed on the view is the "Acces Denied" one.

    0_1461490759161_upload-bc0640b1-c835-4d2e-a2e2-6436ee7563d9

    Appart from that, there's no way of changing the error message from the plugin?

    I would like to change it and let the user know why he/she has not permissions according to my plugin checks.

    This way was a bit tricky but it was working on 0.7.x:

    # code block
    postContent.read = false;
    callback(new Error("<script>$('.alert.alert-danger').html('My custom message to show')"), postContent);
    

    It doesn't need to be in this way, i don't mind changing the way the plugin works, serving a custom view or something, the problem is that i need a little suggestion on how to achieve it.

    *For reference: https://github.com/exo-do/nodebb-plugin-tagstitle (library.js line 112).

    Thanks again.

    1 Reply Last reply
    0
  • exodoE Offline
    exodoE Offline
    exodo Plugin & Theme Dev
    wrote on last edited by
    #4

    i can't help you but it should be easy to make a redirect to another template, isn't it?

    1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    wrote on last edited by yariplus
    #5

    Not sure if this is exactly what you want, but I think you can do this.

    // filter:privileges.topics.get
    plugin.privilegesTopicsGet = function(privileges, callback) {
    	//... do thing
    	if (thing) privileges.redirectUrl = 'someurl';
    	callback(null, privileges);
    };
    
    // filter:topic.build
    plugin.topicBuild = function (data, callback) {
    	if (data.templateData.privileges.redirectUrl) {
    		data.res.redirect(data.templateData.privileges.redirectUrl);
    	}else{
    		callback(null, data);
    	}
    };
    
    jareyJ 1 Reply Last reply
    2
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by jarey
    #6

    @yariplus when it try the way that you suggest, when making the redirect i get the following error:

    25/4 17:28 [4568] - error: /api/topic/2/privileges-topic-test
     MongoError: server 127.0.0.1:27017 sockets closed
        at Server.destroy (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:1042:47)
        at Server.close (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\lib\server.js:398:17)
        at Db.close (C:\Users\jose\Documents\GitHub\NodeBB\node_modules\mongodb\lib\db.js:357:19)
        at Object.module.close (C:\Users\jose\Documents\GitHub\NodeBB\src\database\mongo.js:226:6)
        at shutdown (C:\Users\jose\Documents\GitHub\NodeBB\app.js:306:28)
        at process.<anonymous> (C:\Users\jose\Documents\GitHub\NodeBB\app.js:173:3)
        at emitOne (events.js:77:13)
        at process.emit (events.js:169:7)
        at process._fatalException (node.js:224:26)
    

    Any idea why could be the cause? Maybe when the callback is executed on the privilegesTopicsGet method, the sockets are closed?
    Should I try all the logic on the filter:topic.build hook instead of using the filter:privileges.topics.get for the privilege cheking?

    yariplusY 1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    replied to jarey on last edited by
    #7

    @jarey Yeah, probably one of the callbacks is being called twice.

    jareyJ 1 Reply Last reply
    1
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by
    #8

    @yariplus Thank you, will check that. Sorry if the questions are very basic.

    yariplusY 1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    replied to jarey on last edited by
    #9

    @jarey no problem.

    I fixed some typos in my code above, and tested it on my forum, and verified it works. Let me know if you run into any other errors.

    jareyJ 1 Reply Last reply
    3
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by jarey
    #10

    @yariplus said in Noob Plugin question: Alterin error message on filter hook.:

    @jarey no problem.

    I fixed some typos in my code above, and tested it on my forum, and verified it works. Let me know if you run into any other errors.

    I'm feeling pretty stupic, but got to ask again in order to solve my doubts.

    As i said, now i'm only trying to acces a topic, execute a dummy assigment of an url in the privilege object, and then redirect to that url.
    As you pointed out, i try to do exactly that binding my dummy functions to the hooks filter:privileges.topics.get for the url assignment on the privilege object and filter:topic.build for the redirection.

    Well i keep getting the following error on the browser what makes the page stay with the laoding animation forever:

    nodebb.min.js?16247918-a870-4cf5-8ec8-e783ea5bf570:21353 Uncaught TypeError: Cannot read property 'name' of undefined
    

    My dummy code is as follows: (pretty reading on my updated dummy repo https://github.com/jarey/nodebb-plugin-tagstitle )

    // filter:privileges.topics.get
    tagsTitle.privilegesTopicsGet = function(privileges, callback) {
    	var thing = true;
      //Dummy asignment for the redirect url.
    	if (thing){
        privileges.redirectUrl = 'http://localhost:4567/users';
      }
    	callback(null, privileges);
    };
    
    // filter:topic.build
    tagsTitle.topicBuild = function (data, callback) {
    	if (data.templateData.privileges.redirectUrl) {
        //Execute redirect to the new page.
    		data.res.redirect(data.templateData.privileges.redirectUrl);
        //data.template.name= 'topic-error';
    	}else{
    		callback(null, data);
    	}
    };
    

    I really don't know what I'm doing wrong; i've tried populating the data.template.name attribute, but if i try to make the redirect using that, i get the error of the closed sockets again on mongo.

    Any pice of advice is really appreciated.

    Thanks again.

    yariplusY 1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    replied to jarey on last edited by
    #11

    @jarey Try putting just '/users' as the redirectUrl.

    jareyJ 1 Reply Last reply
    1
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by
    #12

    @yariplus Same result:

    nodebb.min.js?16247918-a870-4cf5-8ec8-e783ea5bf570:21353 Uncaught TypeError: Cannot read property 'name' of undefined
    

    The weird thing is that if i refresh the browser with F5, while the page keeps hanging, iget to the /users page correctly :S.
    Thought it could be a debugger thing, but i restarted on dev mode, and the result was the same.

    yariplusY 1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    replied to jarey on last edited by
    #13

    @jarey Yeah, I get the same result. Something with ajaxify doesn't like doing res.redirect

    1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    wrote on last edited by
    #14

    Okay, I understand the error now. When you redirect, the page doesn't know how to get the data for the new url. Instead of redirecting, you'll have to use the Controller for the page you want to redirect to.

    var usersController = require.main.require('./src/controllers').users;
    
    // filter:topic.build
    Plugin.topicBuild = function (data, callback) {
    	if (data.templateData.privileges.redirectUrl) {
    		if (data.templateData.privileges.redirectUrl === '/users') {
    			usersController.getUsersSortedByJoinDate(data.req, data.res);
    			// You could use your custom page's controller here the same way.
    		}else{
    			callback(null, data);
    		}
    	}else{
    		callback(null, data);
    	}
    };
    
    jareyJ 1 Reply Last reply
    2
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by
    #15

    @yariplus I understand, rhanks for the explanation.

    I was wondering if it is possile to redirect to a custom route created in the plugin itself. Should it be the same way? I mean coding a 'local' controller to obtain the data?

    Again thank you very much!

    1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    wrote on last edited by
    #16

    Yep, you can use this function here the same way.

    https://github.com/jarey/nodebb-plugin-tagstitle/blob/master/library.js#L17

    Just declare it at the global scope so that your hook can see it.

    jareyJ 1 Reply Last reply
    2
  • PitaJP Offline
    PitaJP Offline
    PitaJ Global Moderator Plugin & Theme Dev
    wrote on last edited by
    #17

    Oh god, the TABS. Make it stop. MY EYES

    1 Reply Last reply
    -1
  • jareyJ Offline
    jareyJ Offline
    jarey Translator
    replied to yariplus on last edited by jarey
    #18

    @yariplus said in Noob Plugin question: Alterin error message on filter hook.:

    Yep, you can use this function here the same way.

    https://github.com/jarey/nodebb-plugin-tagstitle/blob/master/library.js#L17

    Just declare it at the global scope so that your hook can see it.

    Thank you very much @yariplus ; I achieved what i needed thanks to your help.

    I was able to see my custom page with my custom messages like you pointed out.

    Then, because i only wanted to change the message displayed on the 403 error regarding the logic on my plugin i was able to change it to using another method; only if it could help anyone else, i just imported the helpers module and called the function notAllowed with my custom message:

    var helpers = require.main.require('./src/controllers/helpers');
    
    // filter:topic.build
    tagsTitle.topicBuild = function (data, callback) {
      if (data.templateData.privileges.errorMessage) {
        helpers.notAllowed(data.req,data.res, data.templateData.privileges.errorMessage);
      }else{
        callback(null, data);
      }
    };
    

    Thanks again. I learned a lot of basic stuff just trying your suggestions.

    Kind regards.

    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development