1 64 65 package com.jcorporate.expresso.services.dbobj; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.dbobj.DBField; 69 import com.jcorporate.expresso.core.dbobj.DBObject; 70 71 81 public class RowPermissions extends DBObject { 82 83 public static final String PERMISSIONS_TABLE_NAME = "ROW_PERMISSIONS"; 84 public static final String PERMISSIONS_TABLE_DESCRIP = "RowPermissions"; 85 88 public static final String TABLE_NAME = "TARGET_TABLE"; 89 90 93 public static final String ROW_KEY = "ROW_KEY"; 94 95 98 public static final String OWNER_ID = "OWNER_ID"; 99 100 101 106 public static final String PERMISSIONS = "PERMISSIONS"; 107 108 119 public static final int MAX_KEY_LENGTH = 150; 120 124 public static final int MAX_TABLE_NAME_LENGTH = 20; 125 126 protected static int sMaxKeyLen = MAX_KEY_LENGTH; protected static String sKeyType = DBField.VARCHAR_TYPE; 129 132 public static final int OWNER_WRITE_MASK = 256; 133 public static final int OWNER_READ_MASK = 128; 134 public static final int OWNER_PERMISSION_MASK = 64; 135 public static final int GROUP_READ_MASK = 32; 136 public static final int GROUP_WRITE_MASK = 16; 137 public static final int GROUP_PERMISSION_MASK = 8; 138 public static final int OTHERS_READ_MASK = 4; 139 public static final int OTHERS_WRITE_MASK = 2; 140 public static final int OTHERS_PERMISSION_MASK = 1; 141 142 public static final int OTHERS_READ_AND_GROUP_WRITES_PERMISSIONS = 143 OWNER_READ_MASK 144 + OWNER_WRITE_MASK 145 + OWNER_PERMISSION_MASK 146 + GROUP_READ_MASK 147 + GROUP_WRITE_MASK 148 + GROUP_PERMISSION_MASK 149 + OTHERS_READ_MASK; 150 151 public static final int DEFAULT_PERMISSIONS = OTHERS_READ_AND_GROUP_WRITES_PERMISSIONS; 152 153 public static final int WIDE_OPEN_PERMISSIONS = 154 OWNER_READ_MASK 155 + OWNER_WRITE_MASK 156 + OWNER_PERMISSION_MASK 157 + GROUP_READ_MASK 158 + GROUP_WRITE_MASK 159 + GROUP_PERMISSION_MASK 160 + OTHERS_READ_MASK 161 + OTHERS_WRITE_MASK 162 + OTHERS_PERMISSION_MASK; 163 164 public static final int OWNER_ONLY_PERMISSIONS = 165 OWNER_READ_MASK 166 + OWNER_WRITE_MASK 167 + OWNER_PERMISSION_MASK; 168 169 public static final int OWNER_AND_GROUP_WRITE_PERMISSIONS = 170 OWNER_READ_MASK 171 + OWNER_WRITE_MASK 172 + OWNER_PERMISSION_MASK 173 + GROUP_READ_MASK 174 + GROUP_WRITE_MASK 175 + GROUP_PERMISSION_MASK; 176 177 public static final int GROUP_READ_ONLY_PERMISSIONS = 178 OWNER_READ_MASK 179 + OWNER_WRITE_MASK 180 + OWNER_PERMISSION_MASK 181 + GROUP_READ_MASK; 182 183 186 public static final int GROUP_ONLY_READWRITE_PERMISSIONS = 187 OWNER_READ_MASK 188 + OWNER_WRITE_MASK 189 + OWNER_PERMISSION_MASK 190 + GROUP_WRITE_MASK 191 + GROUP_READ_MASK; 192 193 194 198 public static final String DEFAULT_PERMISSION_GROUP = UserGroup.ALL_USERS_GROUP; 199 200 public RowPermissions() throws DBException { 201 } 202 203 public RowPermissions(String table, String rowKey) 204 throws DBException { 205 if (rowKey == null) { 206 throw new DBException("null row key"); 207 } 208 if (table == null) { 209 throw new DBException("null table name"); 210 } 211 212 if ((rowKey.length() + table.length()) > getMaxKeyLen()) { 214 throw new DBException("Cannot create row permissions for table: " 215 + this.getJDBCMetaData().getTargetTable() + " row: " + rowKey 216 + " because table name + row's ID (PK) exceeds maximum of " + getMaxKeyLen()); 217 } 218 219 setField(TABLE_NAME, table); 220 setField(ROW_KEY, rowKey); 221 } 222 223 229 public boolean canOwnerAdministrate() throws DBException { 230 return true; 231 } 232 233 public boolean canOwnerRead() throws DBException { 234 return (OWNER_READ_MASK & this.permissions()) == OWNER_READ_MASK; 235 } 236 237 public boolean canOwnerWrite() throws DBException { 238 return (OWNER_WRITE_MASK & permissions()) == OWNER_WRITE_MASK; 239 } 240 241 public boolean canOthersAdministrate() throws DBException { 242 return (OTHERS_PERMISSION_MASK & permissions()) == OTHERS_PERMISSION_MASK; 243 } 244 245 public boolean canOthersRead() throws DBException { 246 return (OTHERS_READ_MASK & permissions()) == OTHERS_READ_MASK; 247 } 248 249 public boolean canOthersWrite() throws DBException { 250 return (OTHERS_WRITE_MASK & permissions()) == OTHERS_WRITE_MASK; 251 } 252 253 254 259 protected synchronized void setupFields() throws DBException { 260 setTargetTable(PERMISSIONS_TABLE_NAME); 261 setDescription(PERMISSIONS_TABLE_DESCRIP); 262 addField(TABLE_NAME, DBField.VARCHAR_TYPE, MAX_TABLE_NAME_LENGTH, false, "Targettablename"); 263 267 addField(ROW_KEY, sKeyType, getMaxKeyLen(), false, "Rowkey"); 268 addField(OWNER_ID, DBField.INT_TYPE, 0, true, "Owner"); 269 addField(PERMISSIONS, DBField.INT_TYPE, 0, true, "Permissionbits"); 270 addKey(TABLE_NAME); 271 addKey(ROW_KEY); 272 } 273 274 public int owner() throws DBException { 275 return this.getFieldInt(OWNER_ID); 276 } 277 278 public void owner(int theOwner) throws DBException { 279 setField(OWNER_ID, theOwner); 280 } 281 282 288 public void permissions(int perm) throws DBException { 289 setField(PERMISSIONS, perm); 290 } 291 292 299 public int permissions() throws DBException { 300 if (getField(PERMISSIONS).length() == 0) { 302 return 0; 303 } 304 return getFieldInt(PERMISSIONS); 305 } 306 307 313 public boolean isFresh() throws DBException { 314 return getField(PERMISSIONS).length() == 0; 315 } 316 317 320 public static int getMaxKeyLen() { 321 return sMaxKeyLen; 322 } 323 324 } 325 | Popular Tags |