1 # This authentication controller has a number of functions
2 # [#login] Show the login view and login users
3 # [#create_admin] Create the first user: admin
4 # [#forgot_password] Generate and mail a new password
5 # [#logout] Logs out the current user (clears session)
6 class AuthenticationController < ApplicationController
7 skip_before_filter :authorize
9 # Display the login form and wait for user to enter a name and password.
10 # We then validate these, adding the user object to the session if they authorize.
13 session[:user_id] = nil
15 # Try to get the user with the supplied username and password
16 logged_in_user = User.login(params[:user][:name], params[:login][:password])
18 # Create the session and redirect
19 unless logged_in_user.blank?
20 session[:user_id] = logged_in_user.id
21 jumpto = session[:jumpto] || { :action => 'list', :controller => 'folder' }
22 session[:jumpto] = nil
25 flash.now[:login_error] = 'Invalid username/password combination'
30 # Show a form for creating the first user.
31 # Creates the first user: admin.
32 # Create the first group: admins.
33 # Add the admin to the admins group.
34 # Create the Root folder
35 # Give the admins group CRUD rights to the Root folder.
36 # The newly created admin user will be logged in automatically.
37 # Initialize the Ferret index.
39 # Check if there already is an admin
40 redirect_to(:action => 'login') and return false if User.admin_exists?
43 # Create the object for the administrator user
44 @user = User.create_admin(params[:user][:email], params[:user][:name], params[:user][:password], params[:user][:password_confirmation])
46 # Create Admins group, Root folder and the permissions
48 Group.create_admins_group
49 Folder.create_root_folder
50 GroupPermission.create_initial_permissions
51 session[:user_id] = @user.id # Login
52 redirect_to(:action => 'list', :controller => 'folder')
55 # Create the initial Ferret index for files
56 # (note: The index for Folders was created when we created the Root folder)
61 # Generate/mail a new password for/to users who have forgotten it.
64 # Try to generate and mail a new password
65 result = User.generate_and_mail_new_password(params[:user][:name], params[:user][:email])
67 # Act according to the result
68 if result['flash'] == 'forgotten_notice'
69 flash.now[:forgotten_notice] = result['message']
71 flash[:login_confirmation] = result['message']
72 redirect_to(:action => 'login')
77 # Clear the current session and redirect to the login form.
81 redirect_to :action => 'login'