Forcing UID value


  • @julian so i need to make an hook on getUserDataByField ?


  • @julian I'm trying to understand how make an hook for getUserDataByField, but pheraps i'm too noob, i write what i've done :

    i've added the user create hook in plugin.json :

    { "hook": "filter:user.create", "method": "filterUserCreate" },
    

    than i've added the function to the library lo listen for 'filter:user.create' :

    plugin.filterUserCreate = function (data, callback) {
        data.user.auid = data.data.auid;
        callback(null, data);
    };
    

    I've added the whitelist hook :

    { "hook": "filter:user.whitelistFields", "method": "whitelistFields" },
    

    than i've added the function to the library to listen for 'filter:user.whitelistFields' :

    plugin.whitelistFields = function(data, callback) {
    	data.whitelist.push('auid');
    	callback(null, data);
    };
    

    At this point i've checked and everythings work, on user create i've also the 'auid' fields with relative value, now i need a function to get user data knowing the remote id field 'auid', i've checked in controllers/user.js and i've found 'getUserDataByField' function wich should help me for my pourpose, but it's limited to uid, username and email.
    I've begin adding the hook for getUserDataByField function in plugin.json :

    { "hook": "filter:user.getUserDataByField", "method": "getUserDataByField" }
    

    than i've begin to adding the filter listener in the library :

    plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) {
        if (field === 'auid') {
            user.getUidByAUid(fieldValue, next);
        }
    
    

    Than i've loose my self 😕


  • I'm getting confused , i would need to have a function like getUidByEmail but for my added field AUID :

    User.getUidByEmail = function (email, callback) {
    db.sortedSetScore('email:uid', email.toLowerCase(), callback);
    };

    i need :

    User.getUidByAUID = function (auid, callback) {
    db.sortedSetScore('auid:uid', auid, callback);
    };

    What i've to do to make that function with a plugin ? I've been able to make it work modifing nodebb source user/index.js , but i don't understad how i can do with a plugin without modifing nodebb core.

  • Admin Staff

    @Simos said in Forcing UID value:

    auid:uid

    Just put it in your plugin.

    plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) {
        if (field === 'auid') {
            user.getUidByAUid(fieldValue, next);
        }
    ...
    };
    
    plugin.getUidByAUid= funtion(auid, callback) {
       db.sortedSetScore('auid:uid', auid, callback);
    };
    

  • @baris thanks ! i've do add also:

    var db = module.parent.require('../database');
    

    right ?

  • Global Moderator

    @Simos require.main.require is preferred:

    require.main.require('./src/database')

  • @PitaJ thanks for your hint


  • What i'm doing wrong ? I get error on getUserDataByField :

    var passport = module.parent.require('passport'),
        passportLocal = module.parent.require('passport-local').Strategy,
        plugin = {};
    var async = require('async');
    var request = require('request');
    
    
    plugin.filterUserCreate = function (data, callback) {
        data.user.auid = data.data.auid;
        callback(null, data);
    };
    
    plugin.whitelistFields = function(data, callback) {
    	data.whitelist.push('auid');
    	callback(null, data);
    };
    
    plugin.thirdLogin = function() {
        passport.use(new passportLocal({passReqToCallback: true}, plugin.continueLogin));
    };
    
    
    var db = require.main.require('./src/database');
    
    plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) {
        if (field === 'auid') {
            user.getUidByAUid(fieldValue, next);
        }
    };
    
    plugin.getUidByAUid = function(auid, callback) {
       db.sortedSetScore('auid:uid', auid, callback);
    };
    
    
    plugin.continueLogin = function(req, username, password, next) {
        var user = module.parent.require('../user');
        var userObj = {}
    
    	async.waterfall([
            function (callback) {
                if (!username || !password) {
                    return callback(new Error('Il nome utente o la password non possono essere vuoti'));
                } else {
                    request.post({url:'https://www.simosnap.org/rest/service.php/rlogin', form:{username: username, password: password}}, function(error, response, body) {
                        if (!error && response.statusCode == 200) {
                            const data = JSON.parse(body)
                            //console.log(data);
                            if (data.ERROR == null) {
                                if (data.UNCONFIRMED == null) {
                                    userObj = data
                                    return callback(null, userObj);
                                } else {
                                    //console.log(data.msg)
                                    return callback(new Error("Account non confermato."));
                                }
                            } else {
                                //console.log(data.msg)
                                return callback(new Error("Accesso non riuscito."));
                            }
                        } else {
                            return callback(new Error('Errore di rete...'));
                        }
                        if (error) {
                            return callback(new Error('Errore'));
                        }
                    })
                }
            },
            function (userObj, callback) {
                //user.getUidByAUID(userObj.uid, callback);
               user.getUserDataByField('1', 'auid', userObj.uid, function(err, userData) {
                    console.log(userData.auid);
                	return callback(userData.auid, null);
                });
    
            },
            function (_uid, callback) {
    		console.log(_uid);
            .........
    
    

  • @baris what i'm missing ? it doesn't work , logs say user.getUserDataByField is not a function

  • GNU/Linux Admin

    It'd be plugin.getUserDataByField, as you're defining it in your plugin code.

  • Global Moderator

    @julian I think his problem is actually that he's not importing the user module from NodeBB:

    var user = require.main.require('./src/user');
  • GNU/Linux Admin

    Ah yes, looks like you're right.


  • @PitaJ

    i'm importing it here:
    var user = module.parent.require('../user');

    It's wrong ?

    I've seen that all functions in nodebb/src/user/index.js are working , getUserDataByField is in nodebb/src/controllers/user.js and is not working

  • Global Moderator

    @Simos please don't use module.parent.require, use require.main.require instead, like I said before.

    Yes, you have it in there once, but you should be importing it at a higher level. The way it is, it's only available within that one function, but you use it in a different hook entirely.


  • @PitaJ
    thanks for the explanations, I've started from the original node-plugin-login-mysite, and I know I'm doing some stuff looking at the original code


  • @PitaJ said in Forcing UID value:

    var user = require.main.require('./src/user');

    i've add this, but i've the same error :

    2018-11-27T20:31:09.199Z [11960] - error: uncaughtException: user.getUserDataByField is not a function
    TypeError: user.getUserDataByField is not a function
    at /home/nodebb/NodeBB/node_modules/nodebb-plugin-login-anope/library.js:73:18

  • Global Moderator

    @Simos ah looks like it's just getUserField


  • Yes! I'm going forward !

    Now i can get getUserDataByField function work 😄 !

    I needed :

    var user = require.main.require('./src/user');
    var userController = require.main.require('./src/controllers').user;

    and call userController.getUserDataByField and not user.getUserDataByField

    The bad notice is that it work if i use uid :

    console.log(userObj.uid);
    userController.getUserDataByField('1', 'uid', '13', function(err, userData) {
                    console.log(userData);
    

    and it return :

    46122
    2018-11-27T20:45:39.706Z [12659] - verbose: [plugins/fireHook] {"meta":"filter:user.whitelistFields"}
    2018-11-27T20:45:39.712Z [12659] - verbose: [plugins/fireHook] {"meta":"filter:users.get"}
    2018-11-27T20:45:39.712Z [12659] - verbose: [plugins/fireHook] {"meta":"filter:user.getSettings"}
    2018-11-27T20:45:39.713Z [12659] - verbose: [plugins/fireHook] {"meta":"filter:user.notificationTypes"}
    { uid: 13,
      username: 'antani',
      userslug: 'antani',
      email: undefined,
      'email:confirmed': 0,
      joindate: 1542932961765,
      lastonline: 1543323019093,
      picture: '',
      fullname: undefined,
      location: '',
      birthday: '',
      website: '',
      aboutme: null,
      signature: '',
      uploadedpicture: '',
      profileviews: 0,
      reputation: 0,
      postcount: 0,
      topiccount: 0,
      lastposttime: 0,
      banned: 0,
      'banned:expire': 0,
      status: 'offline',
      flags: null,
      followerCount: 0,
      followingCount: 0,
      'cover:url': null,
      'cover:position': null,
      groupTitle: null,
      auid: '46122',
      groupTitleArray: [],
      'icon:text': 'A',
      'icon:bgColor': '#2196f3',
      joindateISO: '2018-11-23T00:29:21.765Z',
      lastonlineISO: '2018-11-27T12:50:19.093Z',
      banned_until: 0,
      banned_until_readable: 'Not Banned' } 
    

    but not if i use :

                console.log(userObj.uid);
                userController.getUserDataByField('1', 'auid', userObj.uid, function(err, userData) {
                    console.log(userData);
    

    it return null

    46122
    2018-11-27T20:41:57.152Z [12474] - verbose: [plugins/fireHook] {"meta":"filter:user.whitelistFields"}
    2018-11-27T20:41:57.158Z [12474] - verbose: [plugins/fireHook] {"meta":"filter:users.get"}
    2018-11-27T20:41:57.159Z [12474] - verbose: [plugins/fireHook] {"meta":"filter:user.getSettings"}
    2018-11-27T20:41:57.160Z [12474] - verbose: [plugins/fireHook] {"meta":"filter:user.notificationTypes"}
    { uid: 0,
      username: '[[global:former_user]]',
      userslug: '',
      email: undefined,
      'email:confirmed': 0,
      joindate: 0,
      lastonline: 0,
      picture: '',
      fullname: undefined,
      location: null,
      birthday: null,
      website: null,
      aboutme: null,
      signature: null,
      uploadedpicture: null,
      profileviews: 0,
      reputation: 0,
      postcount: 0,
      topiccount: 0,
      lastposttime: 0,
      banned: 0,
      'banned:expire': 0,
      status: null,
      flags: null,
      followerCount: 0,
      followingCount: 0,
      'cover:url': null,
      'cover:position': null,
      groupTitle: '',
      auid: null,
      oldUid: 46122,
      'icon:text': '?',
      'icon:bgColor': '#aaa',
      groupTitleArray: [],
      joindateISO: '',
      lastonlineISO: '',
      banned_until: 0,
      banned_until_readable: 'Not Banned' }
    

    what's wrong now ? the field auid exist and is filled with the right value


  • I've placed a console.log in my filter , it look it never run,

    plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) {
        console.log('filter running')
        if (field === 'auid') {
            user.getUidByAUid(fieldValue, next);
        }
    };
    

    It' wrong the hook ? or is not possible ? i've not found it in hook docs

    { "hook": "filter:userController.getUserDataByField", "method": "getUserDataByField" },
    

  • I've just try to put this function in the plugin and call that directly without hooks,

    plugin.getUidByAUid = function(auid, callback) {
    	if (!auid) {
    		return callback(null, 0);
    	}
        console.log('auid param log:' + auid);
        db.sortedSetScore('auid:uid', auid, callback);
    };
    
    plugin.getUserDataByField = function (callerUid, field, fieldValue, callback) {
    	async.waterfall([
    		function (next) {
    			if (field === 'uid') {
    				next(null, fieldValue);
    			} else if (field === 'auid') {
    				plugin.getUidByAUid(fieldValue, next);
    			} else if (field === 'username') {
    				user.getUidByUsername(fieldValue, next);
    			} else if (field === 'email') {
    				user.getUidByEmail(fieldValue, next);
    			} else {
    				next(null, null);
    			}
    		},
    		function (uid, next) {
    			if (!uid) {
    				return next(null, null);
    			}
    			userController.getUserDataByUID(callerUid, uid, next);
    		},
    	], callback);
    };
    
    

    than i've try each field using plugin.getUserDataByField and not userController.getUserDataByField (hook doesn't exist so was ignored) :

            function (userObj, callback) {
                //user.getUidByEmail(String(userObj.email), callback)
                //user.getUserDataByUID('1','4', callback);
                //plugin.getUidByAUid(String(userObj.uid), callback);
                console.log(userObj.uid);
                plugin.getUserDataByField('1', 'auid', userObj.uid, function(err, userData) {
                    console.log(userData.auid);
                	//return callback(userData.auid, null);
                });
    

    Using all field work except that auid , db.sortedSetScore('auid:uid', auid, callback) return always null 😕

Suggested Topics

| |