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 User.logged_in_user = logged_in_user
22 jumpto = { :action => 'list', :controller => 'frontpage' }
23 session[:jumpto] = nil
26 flash.now[:login_error] = 'Invalid username/password combination'
31 # Show a form for creating the first user.
32 # Creates the first user: admin.
33 # Create the first group: admins.
34 # Add the admin to the admins group.
35 # Create the Root folder
36 # Give the admins group CRUD rights to the Root folder.
37 # The newly created admin user will be logged in automatically.
38 # Initialize the Ferret index.
40 # Check if there already is an admin
41 redirect_to(:action => 'login') and return false if User.admin_exists?
44 # Create the object for the administrator user
45 @user = User.create_admin(params[:user][:email], params[:user][:name], params[:user][:password], params[:user][:password_confirmation])
47 # Create Admins group, Root folder and the permissions
49 Group.create_admins_group
50 Folder.create_root_folder
51 GroupPermission.create_initial_permissions
52 session[:user_id] = @user.id # Login
53 User.logged_in_user = @user
54 redirect_to(:action => 'list', :controller => 'folder')
57 # Create the initial Ferret index for files
58 # (note: The index for Folders was created when we created the Root folder)
63 # Generate/mail a new password for/to users who have forgotten it.
66 # Try to generate and mail a new password
67 result = User.generate_and_mail_new_password(params[:user][:name], params[:user][:email])
69 # Act according to the result
70 if result['flash'] == 'forgotten_notice'
71 flash.now[:forgotten_notice] = result['message']
73 flash[:login_confirmation] = result['message']
74 redirect_to(:action => 'login')
79 # Clear the current session and redirect to the login form.
83 redirect_to :action => 'login'