Extend update function in users-permissions

Hi guys,

I’m trying to update the update function in the. users-permissions plugin. I’m using strapi 4.6.0.

I’ve created a file strapi-server.js in the folder src/extensions/users-permissions/ but it doesn’t work:

module.exports = (plugin) => {
  plugin.controllers.user.update = async (ctx) => {
    console.log("Updated")
  };
  return plugin;
};

Has anybody an idea?

Thank you!

Best,
Thorsten

Does nobody have a hint for me? :frowning: Thanks!

Okay, for others who have the same problem. I found another solution. Now I’m using in src/index.js the bootstrap({ strapi }) {} function.

In my case the code looks like this:

    strapi.db.lifecycles.subscribe({
      models: ['plugin::users-permissions.user'],
      async afterCreate(event) {
        // Save new internal user id to database
        let sql = `SELECT internal_user_id
                   FROM up_users
                   ORDER BY internal_user_id DESC LIMIT 1`

        let last_internal_user_id = await strapi.db.connection.raw(sql)
        last_internal_user_id = last_internal_user_id[0][0].internal_user_id
        last_internal_user_id++

        sql = `UPDATE up_users
               SET internal_user_id = ${last_internal_user_id}
               WHERE id = ${event.result.id}`

        await strapi.db.connection.raw(sql)

        // Add user and club to Club_User database
        if (event.params.data.club_id.connect.length > 0) {
          await Promise.all(event.params.data.club_id.connect.map(async (club) => {
            sql = `INSERT INTO club_users
                   (user_id, club_id, is_admin, membership_starts, membership_ends)
                   VALUES (${event.result.id}, ${club.id}, false, NOW(), NOW())`

            await strapi.db.connection.raw(sql)
          }))
        }

        return true
      },
      async beforeCreate(event) {
        console.log('beforeCreate', event)
      },
    });
  },```

I think it should be something like this:

//src/extensions/users-permissions/strapi-server.js

module.exports = (plugin) => {
  // Get the original user controller
  const original_user_controller = plugin.controllers.user;
  // Overwrite update method
  plugin.controllers.user = ({ strapi }) => {
    const update = async (ctx) => {
      // Custom code here;
    };
    return {
      original_user_controller,
      update,
    };
  };
  return plugin;
};

Why are you meeting the need to overwrite the update? What’s your goal?

Thanks for the snippet. For the others, there is one error.

module.exports = (plugin) => {
  // Get the original user controller
  const original_user_controller = plugin.controllers.user;
  // Overwrite update method
  plugin.controllers.user = ({ strapi }) => {
    const update = async (ctx) => {
      // Custom code here;
    };
    return {
      // there was missing spread operator (...)
      ...original_user_controller,
      update,
    };
  };
  return plugin;
};