How to Access Current_User Object in Model

How to access current_user object in model?

current_user is not available in any model, to access current_user in model do this

In your application controller

before_filter :set_current_user

def set_current_user
Team.current_user = current_user
end

in your Team model add this line

cattr_accessor :current_user

Congrats, now current_user is available in every model, to get the current user just use the following line everywhere

Team.current_user

NOTE: Restart the server after adding the lines mentioned above!

Now in your question you can use it like

def set_default_url
if Team.current_user.id == self.user_id
"/assets/default_black_:style_logo.jpg"
else
"/assets/default_:style_logo.jpg"
end
end

Hope this helps!

Rails: Access to current_user from within a model in Ruby on Rails

I'd say your instincts to keep current_user out of the model are correct.

Like Daniel I'm all for skinny controllers and fat models, but there is also a clear division of responsibilities. The purpose of the controller is to manage the incoming request and session. The model should be able to answer the question "Can user x do y to this object?", but it's nonsensical for it to reference the current_user. What if you are in the console? What if it's a cron job running?

In many cases with the right permissions API in the model, this can be handled with one-line before_filters that apply to several actions. However if things are getting more complex you may want to implement a separate layer (possibly in lib/) that encapsulates the more complex authorization logic to prevent your controller from becoming bloated, and prevent your model from becoming too tightly coupled to the web request/response cycle.

Get current user from inside the model in Sails

Your asking about reading a session inside the model call. Currently the way sails and waterline are built you can not do this.

You can use the select property on your initial model call to restrict the columns returned. Since this would be in the context of your controller you would have access to the req object.

Here are a bunch of related questions / answers on this topic.

sails.js Use session param in model

Is it possible to access a session variable directly in a Model in SailsJS

https://github.com/balderdashy/waterline/issues/556

https://github.com/balderdashy/waterline/pull/787

Sails Google Group Discussion on the topic

Django: Show the content of the model as per the current user logged in

If you want to fetch data that are related to logged in users then you could use related_name to fetch data related to that user.

Since you have specified related_name to "User" in "Sign" as 'hiuser'.

request.user.hiuser.all() should give all the related "Sign" object of the user. In case if you haven't specified related name then you could use request.user.sign_set.all()

the default related_name pattern is modelname_set

So your view becomes:

    #bookings = Sign.objects.filter(user=User.objects.get(username=request.user)) # instead of this
bookines = request.user.hiuser.all()
return render(request, 'datas/final.html', {'bookings': bookings})

How to get current user in django model?

It should be:

return 'graphs/documents/{0}/email/{1}_{2}'.format(instance.owner.id, now, filename)


Related Topics



Leave a reply



Submit