001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019package org.apache.james.user.lib; 020 021import org.apache.james.user.api.UsersRepository; 022import org.apache.james.user.api.UsersRepositoryException; 023import org.apache.james.user.api.UsersRepositoryManagementMBean; 024import org.apache.james.user.api.model.JamesUser; 025import org.apache.james.user.api.model.User; 026 027import javax.inject.Inject; 028import javax.management.NotCompliantMBeanException; 029import javax.management.StandardMBean; 030import java.util.ArrayList; 031import java.util.Iterator; 032import java.util.List; 033 034@SuppressWarnings("deprecation") 035public class UsersRepositoryManagement extends StandardMBean implements UsersRepositoryManagementMBean { 036 037 /** 038 * The administered UsersRepository 039 */ 040 private UsersRepository usersRepository; 041 042 @Inject 043 public void setUsersRepository(UsersRepository usersRepository) { 044 this.usersRepository = usersRepository; 045 } 046 047 public UsersRepositoryManagement() throws NotCompliantMBeanException { 048 super(UsersRepositoryManagementMBean.class); 049 } 050 051 private JamesUser getJamesUser(String userName) throws UsersRepositoryException { 052 User baseuser = usersRepository.getUserByName(userName); 053 if (baseuser == null) 054 throw new IllegalArgumentException("user not found: " + userName); 055 if (!(baseuser instanceof JamesUser)) 056 throw new IllegalArgumentException("user is not of type JamesUser: " + userName); 057 058 return (JamesUser) baseuser; 059 } 060 061 @Override 062 public void addUser(String userName, String password) throws Exception { 063 try { 064 usersRepository.addUser(userName, password); 065 } catch (UsersRepositoryException e) { 066 throw new Exception(e.getMessage()); 067 } 068 } 069 070 @Override 071 public void deleteUser(String userName) throws Exception { 072 try { 073 usersRepository.removeUser(userName); 074 } catch (UsersRepositoryException e) { 075 throw new Exception(e.getMessage()); 076 } 077 } 078 079 @Override 080 public boolean verifyExists(String userName) throws Exception { 081 try { 082 return usersRepository.contains(userName); 083 } catch (UsersRepositoryException e) { 084 throw new Exception(e.getMessage()); 085 } 086 } 087 088 @Override 089 public long countUsers() throws Exception { 090 try { 091 return usersRepository.countUsers(); 092 } catch (UsersRepositoryException e) { 093 throw new Exception(e.getMessage()); 094 } 095 } 096 097 @Override 098 public String[] listAllUsers() throws Exception { 099 List<String> userNames = new ArrayList<String>(); 100 try { 101 for (Iterator<String> it = usersRepository.list(); it.hasNext(); ) { 102 userNames.add(it.next()); 103 } 104 } catch (UsersRepositoryException e) { 105 throw new Exception(e.getMessage()); 106 107 } 108 return userNames.toArray(new String[userNames.size()]); 109 } 110 111 @Override 112 public void setPassword(String userName, String password) throws Exception { 113 try { 114 User user = usersRepository.getUserByName(userName); 115 if (user == null) 116 throw new UsersRepositoryException("user not found: " + userName); 117 if (!user.setPassword(password)) { 118 throw new UsersRepositoryException("Unable to update password for user " + user); 119 } 120 usersRepository.updateUser(user); 121 } catch (UsersRepositoryException e) { 122 throw new Exception(e.getMessage()); 123 124 } 125 126 } 127 128 @Override 129 public void unsetAlias(String userName) throws Exception { 130 try { 131 JamesUser user = getJamesUser(userName); 132 if (!user.getAliasing()) { 133 throw new UsersRepositoryException("User " + user + " is no alias"); 134 } 135 user.setAliasing(false); 136 usersRepository.updateUser(user); 137 } catch (UsersRepositoryException e) { 138 throw new Exception(e.getMessage()); 139 } 140 } 141 142 @Override 143 public String getAlias(String userName) throws Exception { 144 try { 145 JamesUser user = getJamesUser(userName); 146 if (!user.getAliasing()) { 147 return null; 148 } 149 return user.getAlias(); 150 } catch (UsersRepositoryException e) { 151 throw new Exception(e.getMessage()); 152 153 } 154 } 155 156 @Override 157 public void unsetForwardAddress(String userName) throws Exception { 158 try { 159 JamesUser user = getJamesUser(userName); 160 if (!user.getForwarding()) { 161 throw new UsersRepositoryException("User " + user + " is no forward"); 162 } 163 user.setForwarding(false); 164 usersRepository.updateUser(user); 165 } catch (UsersRepositoryException e) { 166 throw new Exception(e.getMessage()); 167 } 168 } 169 170 @Override 171 public String getForwardAddress(String userName) throws Exception { 172 try { 173 JamesUser user = getJamesUser(userName); 174 if (!user.getForwarding()) { 175 return null; 176 } 177 return user.getForwardingDestination().toString(); 178 } catch (UsersRepositoryException e) { 179 throw new Exception(e.getMessage()); 180 } 181 } 182 183 @Override 184 public boolean getVirtualHostingEnabled() throws Exception { 185 try { 186 return usersRepository.supportVirtualHosting(); 187 } catch (UsersRepositoryException e) { 188 throw new Exception(e.getMessage()); 189 } 190 } 191}