/**
* Copyright 2012-214 Jorge Aliss (jaliss at gmail dot com) - twitter: @jaliss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package controllers;
import play.Logger;
import play.libs.F;
import play.mvc.Controller;
import play.mvc.Result;
import securesocial.core.BasicProfile;
import securesocial.core.RuntimeEnvironment;
import securesocial.core.java.SecureSocial;
import securesocial.core.java.SecuredAction;
import securesocial.core.java.UserAwareAction;
import service.DemoUser;
import views.html.index;
import views.html.linkResult;
/**
* A sample controller
*/
public class Application extends Controller {
public static Logger.ALogger logger = Logger.of("application.controllers.Application");
private RuntimeEnvironment env;
/**
* A constructor needed to get a hold of the environment instance.
* This could be injected using a DI framework instead too.
*
* @param env
*/
public Application(RuntimeEnvironment env) {
this.env = env;
}
/**
* This action only gets called if the user is logged in.
*
* @return
*/
@SecuredAction
public Result index() {
if(logger.isDebugEnabled()){
logger.debug("access granted to index");
}
DemoUser user = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);
return ok(index.render(user, SecureSocial.<DemoUser>env()));
}
@UserAwareAction
public Result userAware() {
DemoUser demoUser = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);
String userName ;
if ( demoUser != null ) {
BasicProfile user = demoUser.main;
if ( user.firstName().isDefined() ) {
userName = user.firstName().get();
} else if ( user.fullName().isDefined()) {
userName = user.fullName().get();
} else {
userName = "authenticated user";
}
} else {
userName = "guest";
}
return ok("Hello " + userName + ", you are seeing a public page");
}
@SecuredAction(authorization = WithProvider.class, params = {"twitter"})
public Result onlyTwitter() {
return ok("You are seeing this because you logged in using Twitter");
}
@SecuredAction
public Result linkResult() {
DemoUser current = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);
return ok(linkResult.render(current, current.identities));
}
/**
* Sample use of SecureSocial.currentUser. Access the /current-user to test it
*/
public F.Promise<Result> currentUser() {
return SecureSocial.currentUser(env).map( new F.Function<Object, Result>() {
@Override
public Result apply(Object maybeUser) throws Throwable {
String id;
if ( maybeUser != null ) {
DemoUser user = (DemoUser) maybeUser;
id = user.main.userId();
} else {
id = "not available. Please log in.";
}
return ok("your id is " + id);
}
});
}
}