Current user id in plugin.rb

(Joe Buhlig) #1

I apologize if I’ve placed this in the wrong category as I’m new to Discourse and meta.

I’ve been working on a development version of Discourse and I’m really loving the application. I’m working to build an integration with Braintree, but part of the process requires me to get the user_id of the current user. I couldn’t find any documentation on how to call the User object from Ruby. There’s a number of cases that show how to do this on the Ember side, but I couldn’t find any on the Rails side. I’m probably just being dense and I’m missing something simple, but my brain can’t see it.

(cpradio) #2

Based on

It seems to just be current_user

(Alan Tan) #3

In your controllers, you can get the current user with current_user

(Joe Buhlig) #4

Doh! I knew it was simple, but just couldn’t see it. I was trying to pull it with the model for some reason. Thank you much!

(Joe Buhlig) #5

I’m still missing something. It still tells me that current_user is undefined. Any ideas on what’s still missing? I thought I understood how Discourse was doing this, but apparently it’s more complicated than I thought. This is in plugin.rb.

PLUGIN_NAME = "braintree_integration".freeze

after_initialize do
  module ::BraintreeSetup
    class Engine < ::Rails::Engine
      engine_name PLUGIN_NAME
      isolate_namespace BraintreeSetup

  require "current_user"
  require_dependency "application_controller"
  class BraintreeSetup::SetupController < ::ApplicationController
    puts current_user.user_id

(Régis Hanol) #6

You can only access current_user inside an action.

(Alan Tan) #7

It might help if you read through this Action Controller Overview — Ruby on Rails Guides. :smiley:

(Joe Buhlig) #8

The action part makes sense. Is there an easy way to run the action from there? I’ve tried before_action on it, but it fails. This is something I’ll want to run every time the user logs in.

Thanks for the article, @tgxworld. I’m definitely new(ish) to Rails.

(Joe Buhlig) #9

Well, I think I’m a failure at Rails. I’ve built a few Rails apps before and never had this much trouble.

I have a Ruby API that I’m trying to pass the user_id into. It’s all server side, so I don’t think Ember applies. I want to validate an account through the external api with the user_id and based on the result, alter whether the user has access to a group or not. I don’t need anything passed to the interface.

I’m not sure a controller is needed as I don’t have a view involved. It’s a really simple process if I can get the user_id.

Anyone willing to save the day?

(Mittineague) #10

I’m not clear on what you’re doing so you may be right.

I was experimenting a while back with getting user info and when I stopped had this in an es6 file.

    if(Discourse.User) {
      if(Discourse.User.current()) {
        Discourse.User.current().findDetails().then(function(u) {

where user info can be accessed inside the function like

(Joe Buhlig) #11

I was able to do that part. My head at least works on that side. I just haven’t succeeded in running it entirely in plugin.rb every time the page loads. I’m now exploring a Sidekiq job possibility.

(Kane York) #12

Well, in what contexts do you have a “current user”? You don’t have one when the plugin is starting up & defining stuff, you only have it during a request…

(Joe Buhlig) #13

@riking You’re awesome! That’s exactly my problem. I was thinking about it wrong. It makes a lot more sense to run a Sidekiq job that does all the work. Thank you for helping me think differently!