1 5 package com.opensymphony.user.provider.file; 6 7 import com.opensymphony.user.Entity; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Properties ; 17 18 19 22 abstract public class FileAccessProvider implements com.opensymphony.user.provider.AccessProvider { 23 25 protected static final Log log = LogFactory.getLog(FileAccessProvider.class); 26 27 29 protected FileGroupsCache groupCache; 30 31 33 public boolean addToGroup(String username, String groupname) { 34 if (!inGroup(username, groupname)) { 35 boolean rv = getGroup(groupname).users.add(username); 36 37 return rv && groupCache.store(); 38 } 39 40 return false; 41 } 42 43 public boolean create(String name) { 44 if (groupCache.groups.containsKey(name)) { 45 return false; 46 } 47 48 FileGroup group = new FileGroup(); 49 group.name = name; 50 groupCache.groups.put(name, group); 51 52 return groupCache.store(); 53 } 54 55 public void flushCaches() { 56 groupCache.store(); 57 } 58 59 public boolean handles(String name) { 60 if (groupCache == null) { 61 return false; 62 } 63 return groupCache.groups.containsKey(name); 64 72 73 85 } 86 87 public boolean inGroup(String username, String groupname) { 88 FileGroup group = getGroup(groupname); 89 90 return (group != null) && group.users.contains(username); 91 } 92 93 public boolean init(Properties properties) { 94 return true; 95 } 96 97 public List list() { 98 return Collections.unmodifiableList(new ArrayList (groupCache.groups.keySet())); 99 } 100 101 public List listGroupsContainingUser(String username) { 102 List result = new ArrayList (); 103 Iterator i = groupCache.groups.keySet().iterator(); 104 105 while (i.hasNext()) { 106 String currentGroup = (String ) i.next(); 107 108 if (inGroup(username, currentGroup)) { 109 result.add(currentGroup); 110 } 111 } 112 113 return Collections.unmodifiableList(result); 114 } 115 116 public List listUsersInGroup(String groupname) { 117 FileGroup g = getGroup(groupname); 118 119 if (g == null) { 120 return Collections.EMPTY_LIST; 121 } 122 123 return Collections.unmodifiableList(getGroup(groupname).users); 124 } 125 126 public boolean load(String name, Entity.Accessor accessor) { 127 accessor.setMutable(true); 128 129 return true; 130 } 131 132 public boolean remove(String name) { 133 boolean rv = groupCache.groups.remove(name) != null; 134 135 return rv && groupCache.store(); 136 } 137 138 public boolean removeFromGroup(String username, String groupname) { 139 boolean rv = getGroup(groupname).users.remove(username); 140 141 return rv && groupCache.store(); 142 } 143 144 public boolean store(String name, Entity.Accessor accessor) { 145 return groupCache.store(); 146 } 147 148 private FileGroup getGroup(String groupname) { 149 FileGroup group = (FileGroup) groupCache.groups.get(groupname); 150 151 return group; 152 } 153 } 154 | Popular Tags |