-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from cferbar/master
Issue #353: Enable anonymous access
- Loading branch information
Showing
8 changed files
with
309 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package streama | ||
|
||
|
||
import grails.converters.JSON | ||
import grails.plugin.springsecurity.SpringSecurityUtils | ||
import org.springframework.security.access.annotation.Secured | ||
import org.springframework.security.authentication.AccountExpiredException | ||
import org.springframework.security.authentication.AuthenticationTrustResolver | ||
import org.springframework.security.authentication.CredentialsExpiredException | ||
import org.springframework.security.authentication.DisabledException | ||
import org.springframework.security.authentication.LockedException | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.web.WebAttributes | ||
|
||
import javax.servlet.http.HttpServletResponse | ||
import javax.servlet.http.HttpSession | ||
|
||
@Secured('permitAll') | ||
class LoginController { | ||
|
||
/** Dependency injection for the authenticationTrustResolver. */ | ||
AuthenticationTrustResolver authenticationTrustResolver | ||
|
||
/** Dependency injection for the springSecurityService. */ | ||
def springSecurityService | ||
|
||
/** Dependency injection for the settingsService. */ | ||
def settingsService | ||
|
||
def login() { | ||
SecurityContextHolder.clearContext(); | ||
|
||
HttpSession session = request.getSession(false); | ||
if (session != null) { | ||
session.invalidate(); | ||
} | ||
|
||
def conf = getConf() | ||
|
||
if (springSecurityService.isLoggedIn()) { | ||
redirect uri: conf.successHandler.defaultTargetUrl | ||
return | ||
} | ||
|
||
String postUrl = request.contextPath + conf.apf.filterProcessesUrl | ||
render view: 'auth', model: [postUrl: postUrl, | ||
rememberMeParameter: conf.rememberMe.parameter, | ||
usernameParameter: conf.apf.usernameParameter, | ||
passwordParameter: conf.apf.passwordParameter, | ||
gspLayout: conf.gsp.layoutAuth] | ||
|
||
} | ||
|
||
/** Default action; redirects to 'defaultTargetUrl' if logged in, /login/auth otherwise. */ | ||
def index() { | ||
|
||
if (springSecurityService.isLoggedIn()) { | ||
redirect uri: conf.successHandler.defaultTargetUrl | ||
} | ||
else { | ||
redirect action: 'auth', params: params | ||
} | ||
} | ||
|
||
/** Show the login page. */ | ||
def auth() { | ||
|
||
/** Check if anonymous access is enabled, to avoid login **/ | ||
if (settingsService.anonymousAccess) { | ||
User anonymous = User.findByUsername("anonymous") | ||
springSecurityService.reauthenticate(anonymous.username,anonymous.password) | ||
} | ||
|
||
def conf = getConf() | ||
|
||
if (springSecurityService.isLoggedIn()) { | ||
redirect uri: conf.successHandler.defaultTargetUrl | ||
return | ||
} | ||
|
||
String postUrl = request.contextPath + conf.apf.filterProcessesUrl | ||
render view: 'auth', model: [postUrl: postUrl, | ||
rememberMeParameter: conf.rememberMe.parameter, | ||
usernameParameter: conf.apf.usernameParameter, | ||
passwordParameter: conf.apf.passwordParameter, | ||
gspLayout: conf.gsp.layoutAuth] | ||
} | ||
|
||
/** The redirect action for Ajax requests. */ | ||
def authAjax() { | ||
response.setHeader 'Location', conf.auth.ajaxLoginFormUrl | ||
render(status: HttpServletResponse.SC_UNAUTHORIZED, text: 'Unauthorized') | ||
} | ||
|
||
/** Show denied page. */ | ||
def denied() { | ||
if (springSecurityService.isLoggedIn() && authenticationTrustResolver.isRememberMe(authentication)) { | ||
// have cookie but the page is guarded with IS_AUTHENTICATED_FULLY (or the equivalent expression) | ||
redirect action: 'full', params: params | ||
return | ||
} | ||
|
||
[gspLayout: conf.gsp.layoutDenied] | ||
} | ||
|
||
/** Login page for users with a remember-me cookie but accessing a IS_AUTHENTICATED_FULLY page. */ | ||
def full() { | ||
def conf = getConf() | ||
render view: 'auth', params: params, | ||
model: [hasCookie: authenticationTrustResolver.isRememberMe(authentication), | ||
postUrl: request.contextPath + conf.apf.filterProcessesUrl, | ||
rememberMeParameter: conf.rememberMe.parameter, | ||
usernameParameter: conf.apf.usernameParameter, | ||
passwordParameter: conf.apf.passwordParameter, | ||
gspLayout: conf.gsp.layoutAuth] | ||
} | ||
|
||
/** Callback after a failed login. Redirects to the auth page with a warning message. */ | ||
def authfail() { | ||
|
||
String msg = '' | ||
def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION] | ||
if (exception) { | ||
if (exception instanceof AccountExpiredException) { | ||
msg = message(code: 'springSecurity.errors.login.expired') | ||
} | ||
else if (exception instanceof CredentialsExpiredException) { | ||
msg = message(code: 'springSecurity.errors.login.passwordExpired') | ||
} | ||
else if (exception instanceof DisabledException) { | ||
msg = message(code: 'springSecurity.errors.login.disabled') | ||
} | ||
else if (exception instanceof LockedException) { | ||
msg = message(code: 'springSecurity.errors.login.locked') | ||
} | ||
else { | ||
msg = message(code: 'springSecurity.errors.login.fail') | ||
} | ||
} | ||
|
||
if (springSecurityService.isAjax(request)) { | ||
render([error: msg] as JSON) | ||
} | ||
else { | ||
flash.message = msg | ||
redirect action: 'auth', params: params | ||
} | ||
} | ||
|
||
/** The Ajax success redirect url. */ | ||
def ajaxSuccess() { | ||
render([success: true, username: authentication.name] as JSON) | ||
} | ||
|
||
/** The Ajax denied redirect url. */ | ||
def ajaxDenied() { | ||
render([error: 'access denied'] as JSON) | ||
} | ||
|
||
protected Authentication getAuthentication() { | ||
SecurityContextHolder.context?.authentication | ||
} | ||
|
||
protected ConfigObject getConf() { | ||
SpringSecurityUtils.securityConfig | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<header class="main" ng-if="!isCurrentState('player')"> | ||
<div class="pull-left flex"> | ||
<a class="logo" ui-sref="dash"> | ||
<img ng-show="$root.getSetting('logo')" ng-src="{{$root.getSetting('logo').value}}" src="/assets/logo.png" alt="${streama.Settings.findByName('title').value}"> | ||
<div ng-show="$root.getSetting('show_version_num').value == true" class="version">v${grailsApplication.metadata.getApplicationVersion()}</div> | ||
<div class="spinner" ng-show="baseData.loading"> | ||
<div class="bounce1"></div> | ||
<div class="bounce2"></div> | ||
<div class="bounce3"></div> | ||
</div> | ||
</a> | ||
|
||
<div class="browse-genres" ng-if="isCurrentState('dash') && genres.length"> | ||
<button class="btn btn-link toggle-menu" ng-click="toggleGenreMenu()"> | ||
<span ng-if="selectedGenre" ng-bind="selectedGenre.name"></span> | ||
<span ng-if="!selectedGenre">{{'DASHBOARD.BROWSE_GENRES' | translate}}</span> | ||
<i class="ion-android-arrow-dropdown"></i> | ||
</button> | ||
|
||
<div class="toggle-menu-content" ng-show="genreMenuOpen"> | ||
<i class="ion-close-circled pull-right" ng-click="toggleGenreMenu()"></i> | ||
<ul> | ||
<li> | ||
<a ng-click="changeGenre()"><i class="ion-grid"></i> All</a> | ||
</li> | ||
<li ng-repeat="genre in ::genres"> | ||
<a ng-click="changeGenre(genre)" ng-bind="::genre.name"></a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> | ||
<ul class="nav navbar-nav"> | ||
|
||
<li ng-if="isCurrentState('dash')"> | ||
<div class="dash-search form-group has-feedback"> | ||
<input type="text" placeholder="Search.." class="form-control input-xs" ng-model="dashSearch" | ||
typeahead-append-to-body="true" uib-typeahead="(item.title || item.name) for item in searchMedia($viewValue)" | ||
typeahead-on-select="selectFromSearch($item)" typeahead-template-url="/streama/typeahead--media.htm" typeahead-loading="baseData.loading"/> | ||
<span class="form-control-feedback ion-android-search" aria-hidden="true"></span> | ||
</div> | ||
</li> | ||
<sec:ifLoggedIn> | ||
<li><a ui-sref="dash">{{'DASHBOARD.TITLE' | translate}}</a></li> | ||
</sec:ifLoggedIn> | ||
|
||
<sec:ifAnyGranted roles="ROLE_CONTENT_MANAGER"> | ||
<li><a ui-sref="admin.shows">{{'MANAGE_CONTENT' | translate}}</a></li> | ||
</sec:ifAnyGranted> | ||
|
||
<sec:ifAnyGranted roles="ROLE_ADMIN"> | ||
<li><a ui-sref="settings.settings">{{'ADMIN' | translate}}</a></li> | ||
</sec:ifAnyGranted> | ||
|
||
<sec:ifLoggedIn> | ||
<li> | ||
<div class="btn-group" style="margin: 4px 0;"> | ||
<button class="btn btn-primary pull-right" ng-click="loginUser()">{{'LOGIN' | translate}}</button> | ||
</div> | ||
</li> | ||
</sec:ifLoggedIn> | ||
</ul> | ||
</div> | ||
|
||
<i class="ion-navicon navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"></i> | ||
</header> |