Is it possible to use laravel nova tool for not authenticated users? - laravel-nova

I'm trying to create custom reset password view that can include my custom field. I'm able to use this custom field for the tools that authenticated user use. The problems are, 1. How to redefine the parent layout. 2. How to prevent Nova guarding my custom routes to be able to use them by not authenticated user.
I've been researching for days trying to solve this. not sure if it solvable at all.

Related

Ruby on rails. How safe is an admin attribute for user model?

I can't find any definitive answers to this.
I have a user model with devise, and I have added an 'admin' attribute that is a boolean, which defaults to false. Currently I have to go to the console and manually change the attribute to true to give a user this admin status.
My question is, how secure is this? Is there any way for someone to change the status of their own user without accessing the server (Currently my computer)?
Could safety be compromised once I put the site into production? The admin attribute is not a permitted parameter on sign in, sign up or update.
I am asking as I want to add a personal dashboard for myself to view, edit or delete anything I might need to when the site goes into production.
If you use strong params then the risk is only if you allow setting this attribute from any action outside of admin panel.
To be extra safe you can use attr_readonly so you can only set this attribute when object is created.
Considering Devise, safety is the last thing that could go wrong, unless either one of Rails or Devise is broken, which is unlikely.
You have taken sufficient steps like not allowing the admin parameter, which should be enough for pretty much every situation.
Several Rails applications, including GitHub and Shopify have been doing this (possibly without Devise, but that's not a concern) without security issues. I myself use Devise and admin flags and can vouch that it is safe enough.
Any reason you are not using something like RoleModel? It will allow you to expand to other roles in the future in a much more manageable way than adding an attribute to User for each role.
Either way... unless you provide a way for them to update that field, it should be secure. If you want to ensure that there's no way a non-admin could update that field, you could always add something like this to UsersController#update:
params[:user].delete(:admin) unless current_user.admin?

Add specific user roles to a specific model instance using CanCan and Rolify

As I work may way through a Rails app, I have another question. The app allows for users to sign up via Devise. That works. Within the app, users are able to create their own groups called Circles. That works. Now I want the user to be able to add other users to their Circle. This doesn't work.
So far I've implemented CanCan and Rolify. I'm able to assign a role to a user from the User model, so I know the setup is working just fine. What I cannot figure out is how a user can add another user to a specific Circle. I've looked over SO and found something close to what I want to do here: Authorization in Rails 3.1 : CanCan, CanTango, declarative_authorization?, but this doesn't work for my situation.
I know how to add a role at the application level with CanCan. What I want to do is add a role at the specific Circle instance. Not all users will have access to a Circle, only those who are added.
I'm open to other ways to accomplish this if anyone has any ideas. What would be the best way to set this up?
EDIT
I figured out how to do this from rails console. user.add_role :moderator, Circle.find(22) This works perfectly and saves to the database. How can I do this same thing through a form in a view?
Use a controller....it will add the role after that you submit the form! In the controller you can just use a class method for example:)
Let me know if I need to be more explicit!
Cheers

Devise - limit registrations by using the password reset mechanism?

I'm looking for a way to allow private registrations, or registrations that require manual approval. The latter can be done using the strategy as described here, but I figure the former method might be more convenient if I could somehow take advantage of the password reset module to simplify the process (send an email with a one-time use token but for the purpose of account creation). Has anyone attempted anything like this, or possibly have a better strategy that makes greater use of existing components in devise?
possibly related: Ruby on rails: Devise, want to add invite code?
I have to admit I am not a big fan of using features in a framework that are designed for other uses to accomplish some other goal.
What I usually do when I want a private Sign-Up where you have to be invited to the App is to simply put the user creation/registration inside the Application. After all Devise is just an authentication mechanism ontop of the User model.
So in my current app for example there is a explicit way inside the app for existing users to invite a friend.
The inviting User has a form that creates an entry in the Users table for the new guy with his email address and a field that tells me if the user has finished his registration. I create a little Token that also gets saved to the Database (SecureRandom.hex(8) is a nice way to create such Tokens).
The system shoots the new guy a email telling him where to sign up (with a URL that contains the token), and the sign up is just a form that sets password and additional fields.
All of this is no real magic in Rails, it's all in all 2 controller actions, 2 views and 1 mailer to accomplish it and you are in no way constrained by any API Devise is or is not giving you.
I only had to make sure Devise won't authenticate Users that have not yet redeemed their invitation token but that's it then.
Sure not having to write the sign up view is convenient, but especially when you are dealing with partial information (the inviting User in my case has to fill in some information about the new user already) that gets only complemented by the new user it's quite convenient to just have regular forms where you can do anything with them.
Unless someone writes a Gem that extends Devise to do exactly this, I think I'll stick to this approach.
Turns out there was a third strategy - I could simply lock new accounts (lockable, before_create filter) and provide a manual unlock facility.

Devise: Make user login in two ways

I am trying a weird thing in devise. Here I have got two types of login.
1) Default devise login using username and password.
2) Login with user id and password.
The password (password2) in second step is different from that (password1) in first step.
I want to login through both using same interface, i.e. there will be one login page where you need to enter email or user id and corresponding password (password1 or password2 respectively).
Is it possible to do the same in devise?
Thanks
Paritosh
Allowing multiple user identifiers is discussed on the Devise wiki which I have linked here.
Update: However, as I now understand, you want two separate sets of credentials (userid/pw1, and email/pw2) for some reason.
I think the answer to your question is that "while it's possible to accomplish with Devise, it's far more effort to change Devise than it is to write yourself". If you look at the link, there's actually a fair amount of work needed to make the simpler change of accepting either userid or email for the same password. It gets even more complicated when implementing your requirements.
Unless you really create your own system from scratch, either Devise or Rails' built-in has_secure_password both make several assumptions about the name of the attribute holding the password (i.e. that it's called password). And while there's an assumption that there's a (single) model containing the authentication information and this attribute, I see no reason why you couldn't have two models, perhaps both belonging to a User model, each of which provide the basic functionality of encrypting, storing, and validating the attributes for the method the user has used, but for which all of the other functionality is provided by the parent User record, and its controllers and views. Some simple logic in the User model determines which method is being used and farms off that functionality to the appropriate sub-model.
So yeah, it can be done, and I would suggest has_secure_password will be simpler in your unusual case.
But perhaps it's worth asking: if I am the first person to encounter this situation, perhaps there's an alternative that could meet my requirements that follows some existing convention or approach. For example, is this a "single sign-on" interface that provides authentication for several unrelated services? If so, that might be the thing to search for.
here is a complete tutorial to login with both username and email. you can replace username with user_id or whatever you required.

CRUD based authorization?

Does anyone know of a quick way to add a role CRUD system to rails?
I only want admins to create users and have all currently signed up users listed on a page in my app.
Im trying to figure out a way to assign users to different roles and restrict them for performing certain actions using collection select or a series of checkboxes.
Ive followed a few tutorials but none seem to be working for me :/
Can anyone recommend a solution? I us devise for my authorization it that matters.
You can think about CanCan. Using it you're able to define roles and restrict access to certain actions or model elements according to the role.
It can be also easily integrated with Devise mentioned by Scott Schulthess.
Devise on github has a wiki page showing how to do this
https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
You can also try using API Keys if you want to expose this API to many others

Resources