Added remember_me feature to login page.
This commit is contained in:
parent
2a1a78c210
commit
0d26851a5e
4 changed files with 34 additions and 11 deletions
|
@ -11,6 +11,11 @@
|
||||||
<input name="username" type="text" class="input span3" placeholder="Username or email" value="<%= @item.username %>" autocapitalize="off"/>
|
<input name="username" type="text" class="input span3" placeholder="Username or email" value="<%= @item.username %>" autocapitalize="off"/>
|
||||||
<input name="password" type="password" class="input span3" placeholder="Password"/>
|
<input name="password" type="password" class="input span3" placeholder="Password"/>
|
||||||
<button class="btn btn-primary" type="submit">Sign in</button>
|
<button class="btn btn-primary" type="submit">Sign in</button>
|
||||||
|
<div>
|
||||||
|
<span class="small"><input name="remember_me" value="1" type="checkbox"/> Remember me</span>
|
||||||
|
<span class="small">·</span>
|
||||||
|
<a href="#resend_password" class="small">Forgot password?</a>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,6 +13,15 @@ body {
|
||||||
background-image: url("../assets/glyphicons-halflings.png");
|
background-image: url("../assets/glyphicons-halflings.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
.hero-unit .small {
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 20px;
|
||||||
|
color: #999999;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* removed margin of forms to not break the layout with submit buttons within <form></form> area e. g. for modal dialogs
|
* removed margin of forms to not break the layout with submit buttons within <form></form> area e. g. for modal dialogs
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -5,8 +5,7 @@ class SessionsController < ApplicationController
|
||||||
|
|
||||||
# "Create" a login, aka "log the user in"
|
# "Create" a login, aka "log the user in"
|
||||||
def create
|
def create
|
||||||
logger.debug 'session create'
|
|
||||||
# logger.debug params.inspect
|
|
||||||
user = User.authenticate( params[:username], params[:password] )
|
user = User.authenticate( params[:username], params[:password] )
|
||||||
|
|
||||||
# auth failed
|
# auth failed
|
||||||
|
@ -15,32 +14,34 @@ class SessionsController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
user = User.find_fulldata(user.id)
|
||||||
|
|
||||||
# do not show password
|
# do not show password
|
||||||
user['password'] = ''
|
user['password'] = ''
|
||||||
|
|
||||||
user['roles'] = user.roles.select('id, name').where(:active => true)
|
|
||||||
user['groups'] = user.groups.select('id, name').where(:active => true)
|
|
||||||
user['organization'] = user.organization
|
|
||||||
user['organizations'] = user.organizations.select('id, name').where(:active => true)
|
|
||||||
|
|
||||||
# auto population of default collections
|
# auto population of default collections
|
||||||
default_collection = default_collections()
|
default_collection = default_collections()
|
||||||
|
|
||||||
# set session user_id
|
# set session user_id
|
||||||
session[:user_id] = user.id
|
session[:user_id] = user['id']
|
||||||
|
|
||||||
# check logon session
|
# check logon session
|
||||||
logon_session_key = nil
|
logon_session_key = nil
|
||||||
if params['logon_session']
|
if params['logon_session']
|
||||||
logon_session_key = Digest::MD5.hexdigest( rand(999999).to_s + Time.new.to_s )
|
logon_session_key = Digest::MD5.hexdigest( rand(999999).to_s + Time.new.to_s )
|
||||||
ActiveRecord::SessionStore::Session.create(
|
session = ActiveRecord::SessionStore::Session.create(
|
||||||
:session_id => logon_session_key,
|
:session_id => logon_session_key,
|
||||||
:data => {
|
:data => {
|
||||||
:user_id => user.id
|
:user_id => user['id']
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# remember me - set session cookie to expire later
|
||||||
|
if params[:remember_me]
|
||||||
|
request.env['rack.session.options'][:expire_after] = 1.year.from_now
|
||||||
|
end
|
||||||
|
|
||||||
# return new session data
|
# return new session data
|
||||||
render :json => {
|
render :json => {
|
||||||
:session => user,
|
:session => user,
|
||||||
|
@ -92,10 +93,14 @@ class SessionsController < ApplicationController
|
||||||
|
|
||||||
# "Delete" a login, aka "log the user out"
|
# "Delete" a login, aka "log the user out"
|
||||||
def destroy
|
def destroy
|
||||||
|
|
||||||
# Remove the user id from the session
|
# Remove the user id from the session
|
||||||
@_current_user = session[:user_id] = nil
|
@_current_user = session[:user_id] = nil
|
||||||
|
|
||||||
|
# reset session cookie (set :expire_after to '' in case remember_me is active)
|
||||||
|
request.env['rack.session.options'][:expire_after] = ''
|
||||||
|
request.env['rack.session.options'][:renew] = true
|
||||||
|
|
||||||
render :json => { }
|
render :json => { }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,10 @@ class User < ApplicationModel
|
||||||
|
|
||||||
def self.authenticate( username, password )
|
def self.authenticate( username, password )
|
||||||
|
|
||||||
|
# do not authenticate with nothing
|
||||||
|
return if !username
|
||||||
|
return if !password
|
||||||
|
|
||||||
# try to find user based on login
|
# try to find user based on login
|
||||||
user = User.where( :login => username, :active => true ).first
|
user = User.where( :login => username, :active => true ).first
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue