BasicTenantUsers.java
/***************************************************************************
Copyright 2014 Emily Estes
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 net.metanotion.multitenant;
import java.sql.Connection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.sql.DataSource;
import net.metanotion.authident.AuthPassword;
import net.metanotion.authident.Realm;
import net.metanotion.authident.UserToken;
import net.metanotion.formsauth.AuthMailer;
import net.metanotion.formsauth.BulkAccounts;
import net.metanotion.functor.Block;
import net.metanotion.util.JDBCTransaction;
/** This is a reasonable default implementation of the {@link TenantUsers} interface if you're using the built-in
authentication and authorization framework ({@link net.metanotion.authident} and related packages).
*/
public final class BasicTenantUsers implements TenantUsers {
private static final BasicTenantUsersQueries queries = new BasicTenantUsersQueries();
private final DataSource ds;
private final AuthPassword<? extends AuthPassword> pw;
private final Realm r;
private final BulkAccounts ba;
/** Create an instance of the TenantUsers interface for a specific tenant instance.
@param tenantDS The SQL DataSource for the tenant's schema.
@param pw The AuthPassword interface instance for the tenant.
@param r The Realm interface instance for the tenant.
@param mailer The mailer instance for the tenant.
*/
public BasicTenantUsers(final DataSource tenantDS,
final AuthPassword<? extends AuthPassword> pw,
final Realm r,
final AuthMailer mailer) {
this.ds = tenantDS;
this.pw = pw;
this.r = r;
this.ba = new BulkAccounts(ds, r, pw, mailer);
}
@Override public List<String> listRoles() {
return JDBCTransaction.doTX(ds, new Block<Connection,List<String>>() {
@Override public List<String> eval(final Connection conn) throws Exception {
return queries.getRoles(conn);
}
});
}
@Override public List<TenantAccount> getAccounts(final int offset, final int count) {
return JDBCTransaction.doTX(ds, new Block<Connection,List<TenantAccount>>() {
@Override public List<TenantAccount> eval(final Connection conn) throws Exception {
return queries.listTenantAccounts(conn);
}
});
}
@Override public void deleteAccount(final long userId) {
r.getUser(userId).delete();
}
@Override public List<String> addAccounts(final Iterable<String> emails) {
return ba.addAccounts(emails);
}
@Override public void setRoles(final long userId, final Set<String> role) {
ba.updateRoles(r.getUser(userId), role);
}
@Override public void setUsernames(final long userId, final Set<String> emails) {
final UserToken uid = r.getUser(userId);
final Set<String> currentAccounts = new HashSet<String>();
for(final String username: pw.listUsernames(uid)) { currentAccounts.add(username); }
for(final String username: emails) {
if(!currentAccounts.contains(username)) {
pw.addAuthentication(uid, username);
}
}
for(final String username: currentAccounts) {
if(!emails.contains(username)) {
pw.deleteAuthentication(uid, username);
}
}
}
}