I have a goal to have a User model and a Vendor model. Think as if User is a company, and Vendor is employees where the company can create multiple employee accounts to associate to the company.
I have set up Sorcery to have User model and Vendor model but when signing in is where my issues are.
At the end of the sorcery file, there is this:
config.user_class = "User"
I have tried:
config.user_class = "User" || "Vendor"
When signing in, it will only search through the model that is first.
this:
config.user_class = "User"
config.user_class = "Vendor"
(in both orders, vice versa)
Whichever is last, that model works. It will search only through the Vendor model if the Vendor model is last.
I looked into STI, but most of what I have seen is creating associated models that inherit from the one model. Not much on multiple models for authentication for each, or any. Is this a route to go into?
I need both User and Vendor to be able to sign in as separate accounts.
I have, in the past, used Devise to create multiple models with ease but Sorcery seems to be giving issues?
Is there a way to set up Sorcery to allow multiple models?
Sorcery does allow STI though the user.subclasses_inherit_config setting in config/initializers/sorcery.rb. You can set it to true then inherit your Vendor class from User.
I've never used the Gem but I would look at just adding a role to your user class and use authorization.
Related
I've created 2 tables, one for users and one for admins.
I created 2 tables as they both collect different information, but I want to be able to allow a sign in using an email address and password from both the admin and user tables via the same form.
Is this possible? I've looked around and people seem to have created 1 users table and added an admin boolean, but I wanted to avoid this and I didn't want to collect unnecessary data if I didn't need to.
Any help and assistance about how to best go around this would be great.
If you are implementing something from scratch, then it is simply a matter of coding it. I think this approach has some inherent flaws and I would avoid it.
If you want to have some segregation on the model side of things, I suggest you use STI. That way there is some shared behaviour/attributes and the distinctions can be coded separately, so you have your protection.
If you have plenty of distinct attributes, I would suggest separating them from your user/admin and creating an "admin_profile" model that belongs_to :admin and a "user_profile" that belongs_to :user.
And to make coding "transparent", you can create accessors in your admin model class to get/set the profile attributes seamlessly. Say you have an is_cool attribute on the admin_profile model, but you'd like to access it as
imadmin.is_cool
You can have in your admin.rb model
has_one :admin_profile
def is_cool
self.admin_profile.is_cool
end
be careful cause the has_one relationship may return nil if there is no profile associated with the admin/user.
I am using acts_as_tenant gem to do a multi-tenant app. I followed the instructions. I am using sub-domains.
In my application_controller.rb I have:
set_current_tenant_by_subdomain(:account, :subdomain)
I am using Account as the tenant. In my User model I called:
acts_as_tenant(:account)
The Problem
When I log into an account using a subdomain (e.g: john.realestate.dev), everything is OK (current_tenant i.e john is set).
I have another model called Property; when the current logged in tenant i.e john creates a new property, that record is seen by all the other users. I want only john to be able see the record he has created.
Where am I going wrong?
my models relationships are:-
Account - has_many :users
User - belongs_to :account
Property
i fixed my issue by creating a relationship between Account(has_many :properties) and Property(Belongs_to :account).
then i added acts_as_tenant(:account) to the Property Model. I ran a migration that adds account_id to the property model.
now it scopes data only created by the logged in user according to the subdomain.
i fixed it by re-reading the gem's doc multiple times until i made sense of this statement from the doc.
acts_as_tenant requires each scoped model to have a column in its schema linking it to a tenant. Adding acts_as_tenant to your model declaration will scope that model to the current tenant BUT ONLY if a current tenant has been set.
Any ideas, suggestions on my implementation are mob welcome.
The real fix here is to make sure you include acts_as_tenant(:account) on every single model you want scoped.
This is going to make sure it checks for the current_tenant before saving data.
You don't necessarily need the has_many and belongs_to relation, although it sounds like it may make sense for your application.
I have a problem, i need create 3 type of users with devise. these users have different fields. I thought about creating a table but would have many blank fields.
any recommendations? I'm very confused. I need a tutorial :)
Thanks friends.
You are looking for something called Single Table Inheritance. Basically you create one user model with devise which has a column "type" which is a string and you create sub models, like
class Admin < User
end
Class CMS < User
end
etc....
Also, put all the common attributes in the User model
Look at these links for an in-depth explanation. STI is the solution. That much is sure
http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-1/
devise and multiple "user" models
Unique IDs between Users and Admins with Devise Rails
Rails 4 Devise Multiple User Models STI
Stackoverflow ppl has already dealt with STI extensively!
Hope these helps!
Use STI mechanism...
or you can separate all tables with use of devise
Good Afternoon All,
I'm just trying to think the best way to set this up at the beginning rather than changing everything at the end.
I have 3 models. I have a devise generated model called User.rb a model called employer.rb and candidate.rb.
My understanding is that once the user signs up and selects a role I can assign a role type after sign up and redirect them to a page on role where they can fill in the fields.
Each model has different fields and different data requirements.
How would I go about this, any pointers or ideas of how I'd approach this...
This seems like a job for STI (single table inheritance)! Add a type column to your User model, and have the Employer and Candidate models inherit from User. There are a lot of STI with rails references out on the internet, but here's one specific blog post.
I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
Now I need a little help. When adding the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's.
So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table?
Or if I am displaying a post. How will I know which table to look for to get the user or admin info?
I know I can just add a role column to the users table but I wanted to avoid this.
I hope this makes sense haha
I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.
class NormalUser < User
end
class Admin < User
end
The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.
To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser