User.admin with Devise ( Rails 3) - ruby-on-rails-3

This is probably a simple question, but as I have never gone through the process thought I would seek assistance. I am using devise for my user authentication in my rails 3 app. Rather than create a seperate model for my 1 admin user i would like to set a user with admin => TRUE.
I have my User DB setup ready to go ( have added Admin column in the USER DB).
My question is how do i create a User so that the admin flag is set to TRUE. Would this be done in the console, if so what would the command look like?
All help appreciated

Check out section Listing 9.41. of Michael Hartl's tutorial.

User.toggle!(:admin)
or as I prefer:
u = User.find(1)
u.toggle!(:admin)

Related

rails user authentication on mulit tenancy system

Im currently creating a multi tenancy system in rails 5 using the gem Apartment
I have a Tenant model which contains a Name and Tenant so I can create individual tenants (or companies).
Each tenant has a users table. When a user logs in I set a session containing the user_id. The problem is when I switch subdomains it then picks up the user id from the second tenant and shows me logged in as them.
for example: on tenantA im logged in a Brad (user_id:1 on tenantA user table)
when I switch to tenantB im logged in as Dave (user_id:1 on tenantB user table)
Obviously this is no good as you can access data from another tenant.
Im just not really sure how to restrict users access to only their tenant. I think maybe some sort of scope on the session so it only applies the session to the current subdomain and not all of them, but not sure how to do this.
Has anyone done this before that could help me? Not sure what code to paste here but just let me know and I will post my code.
Thanks in advance

rails 5 using devise

hi currently i am making a new project using rails 5, and devise , my problem is that i am trying to get the current user that is logged into my webapp, and then get the current users id and save it into the database.
currently getting a lot of error and having trouble doing so.
my database are the following for the sample
Note this is the default devise setup for the user
User
username
password
email address
blog
title.text
body.text
user_id.integer
now my problem is that how do i get the current user that is logged into my webapp, and then save it into the blog database that contains the user_id. i havent added the blog yet into one of my samples , but i am currently failing at the results that i need to be making and pushing forward into it.
is there a way to scaffold it , or do i need to go commando and hardcode it, if so how is it possible to do so?
get the current user so that it gets the current user id and save it to each blog.
2.how do i push it into the controller
how do i do it into the model as well
i am a bit confused as to how it should be made and done , with rails any help will be appreciated
In your user.rb model
define relationship with user in this way
has_many :blogs
in your bolg.rb model
belongs_to :user
now in your create method in your blog controller
#user = current_user
then
if #user.blogs.create(blog_params)
#your logic
end
This automatically save user_id to database.

Add Spree to rails app with devise

I'm new at RoR but I'm loving every bit of it :)
I have a small app that uses devise for authentication and it's working fine.
Now I want to add a ecommerce part nad I decided for spree. I installed spree and during installation it asked me if I wanted to use the default authentication, I said 'no'. Then it asked me for the user model which I stated 'User'.
Now I enter my app, I login it goes to the products page on spree. That's ok, but when I try to access the admin part, I get redirected to the products page.
My doubts are:
- Should I install anyway the spree_auth_devise gem?
- Should this be a problem with the database and the users role? Because on the "spree_users" table I don't have anything, only on the Users table...
How can I associate one of my existing users to an admin user on spree?
Should this be the problem or I'm missing something else?
Did you run this:
rails g spree:custom_user User
http://guides.spreecommerce.com/authentication.html in the Initial Setup block
To check is user is admin:
user = User.find_by_email("master#example.com")
user.spree_roles << Spree::Role.find_or_create_by_name("admin")
## To test that this has worked, use the has_spree_role? method, like this:
user.has_spree_role?("admin")

Have CanCan use Active Admin user?

I'm using Devise and CanCan for authorization on the frontend of a Rails 3 app. I also have Active Admin as the interface for the backend. I'm trying to create different roles for admins in the backend. Both ends have a login form that uses different 'user' models & tables. The problem is that CanCan fetches the current user from the frontend (grabbing the current user object) and uses that to see if someone in the backend has the correct permissions.
So, how I can have CanCan correctly grab the admin user that's logged in?
If anyone needs more information, I'll be glad to supply it.
I have not used ActiveAdmin before, but have used Devise and Cancan in a couple of projects before.
Having looked at Active Admin Documentation,
Set the method that controllers should call to authenticate the current user with:
# config/initializers/active_admin.rb
config.authentication_method = :authenticate_admin_user!
Set the method to call within the view to access the current admin user
# config/initializers/active_admin.rb
config.current_user_method = :current_admin_user
You can override Cancan behaviour in your application, by looking at :current_admin_user instead of :current_user.
Refer here Cancan changing defaults.
If you still can't get it, post your problems, where you are stuck.

Display realtime list of logged in users ( Maybe using AuthLogic)

I need to display a real-time or at least near real-time list of users currently logged into my site.
I'm using the AuthLogic gem to manage Registration/Login & Session management.
Is there a quick way for me to find a list of current usernames logged into the site using authlogic?
I ask this new question because there seems to be conflicting answers on whether this is possible:
Rails and Authlogic. Show currently logged in users
How to get a list of online users in Authlogic?
Thanks
Ok, I just tried it out there and the following code allows you access to all logged in users and their model attributes.
def online_users
return Member.logged_in
end
#loggedIn = online_users
#loggedIn.each do |member|
print member.username
print member.email
end
You must add the 'last_request_at' authlogic column to your user table.
We can list the list of online users with my rails application by use of active record session store, please see this github app https://github.com/mohanraj-ramanujam/online-users