I would like to display a user account signup form if no user is
present. The signup form will then create a new user account with admin
privileges.
Right now I am creating a pre-defined / hard coded admin account.
Whenever user clicks on a login page, the application checks for an
account with admin privileges. If no such account is found, then I am
creating an admin account. Usually it will occur during first run of the
application.
I would like to prompt user to create an admin account automatically,
i.e., without user having to click on any link. How can I invoke my
signup method in this manner? Where should I define it and call it from?
first of all: what sense makes an admin-account if everybody gets one
automatically?
second: what information do you store? without having the user at
least choose a username, password, how would you recognise which user
is which?
i'd really want to help, but it seems i don't understand what you are
doing here.
Not everyone gets an admin account automatically.
Current approach:
The user already knows username and password of this admin account
(included in README file). When user clicks on the login button, the
application checks if any user with admin privileges exists. If not,
then an account with admin privileges is setup. Admin user can change
his/her password thereafter. On subsequent runs (when admin account is
already exists), another admin account won't be created and there exists
only one admin account. Also, once created the admin account can not be
deleted.
Problem: Every time user clicks on the login button, the method to make
admin account if no exists is called upon.
What I would like to do:
When user runs the application and no admin account exists, then display
a signup/create admin form to create an admin account. Usually this will
occur during first run, but not necessarily.
How can I implement this? Other suggestions are welcome..
What I would like to do:
When user runs the application and no admin account exists, then display
a signup/create admin form to create an admin account.
if i understand correctly, you need to write some code roughly like
the following in your controller:
if User.find(:all, :conditions => {:group_id => Group.find_by_name
("admin").id}).size == 0
# render create_admin_account as none exists so far
else
# admin-account exists => just login
end
Problem: Every time user clicks on the login button, the method to make
admin account if no exists is called upon.
do you already have some code to see where what could be wrong about
it?
now, as this is a class-method self would be the user-class, but not
an instance of it, which means it is not a single user and thus cannot
have the role 'admin'. therefor your if-clause always gets evaluated
to false and you create a new user every time someone hits that
button.
make it an instance-method (= get rid of the 'self.' in method-
definition and adjust the logic accordingly). and call it like this
user = User.authenticate(params[:username], params[:password])
user.make_admin_if_none
another thought:
it would be better to include that kind of logic into the registration-
process. that way you dont have to call it every time someone logs in.
something like this:
def register
user = User.new
admins = Group.find_by_name("admin")
if admins.users.size == 0
user.groups << admins
end
end
hard to tell you which way to go. in the end it's YOUR application.
still i hope i could give you some inspiration.