/**
* Global Sensor Networks (GSN) Source Code
* Copyright (c) 2006-2016, Ecole Polytechnique Federale de Lausanne (EPFL)
*
* This file is part of GSN.
*
* GSN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GSN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GSN. If not, see <http://www.gnu.org/licenses/>.
*
* File: app/service/gsn/GSNUSerServicePlugin.java
*
* @author Julien Eberle
*
*/
package service.gsn;
import models.gsn.auth.User;
import play.Application;
import com.feth.play.module.pa.user.AuthUser;
import com.feth.play.module.pa.user.AuthUserIdentity;
import com.feth.play.module.pa.service.UserServicePlugin;
import com.google.inject.Inject;
public class GSNUserServicePlugin extends UserServicePlugin {
@Inject
public GSNUserServicePlugin(final Application app) {
super(app);
}
@Override
public Object save(final AuthUser authUser) {
final boolean isLinked = User.existsByAuthUserIdentity(authUser);
if (!isLinked) {
return User.create(authUser).id;
} else {
// we have this user already, so return null
return null;
}
}
@Override
public Object getLocalIdentity(final AuthUserIdentity identity) {
// For production: Caching might be a good idea here...
// ...and dont forget to sync the cache when users get deactivated/deleted
final User u = User.findByAuthUserIdentity(identity);
if(u != null) {
return u.id;
} else {
return null;
}
}
@Override
public AuthUser merge(final AuthUser newUser, final AuthUser oldUser) {
if (!oldUser.equals(newUser)) {
User.merge(oldUser, newUser);
}
return oldUser;
}
@Override
public AuthUser link(final AuthUser oldUser, final AuthUser newUser) {
User.addLinkedAccount(oldUser, newUser);
return newUser;
}
@Override
public AuthUser update(final AuthUser knownUser) {
// User logged in again, bump last login date
User.setLastLoginDate(knownUser);
return knownUser;
}
}