1 16 17 package org.apache.cocoon.slide.util; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.Date ; 22 import java.util.Enumeration ; 23 import java.util.List ; 24 25 import org.apache.slide.authenticate.CredentialsToken; 26 import org.apache.slide.common.NamespaceAccessToken; 27 import org.apache.slide.common.SlideToken; 28 import org.apache.slide.common.SlideTokenImpl; 29 import org.apache.slide.content.Content; 30 import org.apache.slide.content.NodeProperty; 31 import org.apache.slide.content.NodeRevisionDescriptor; 32 import org.apache.slide.content.NodeRevisionDescriptors; 33 import org.apache.slide.lock.Lock; 34 import org.apache.slide.lock.NodeLock; 35 import org.apache.slide.macro.Macro; 36 import org.apache.slide.macro.MacroParameters; 37 import org.apache.slide.security.NodePermission; 38 import org.apache.slide.security.Security; 39 import org.apache.slide.structure.ObjectNode; 40 import org.apache.slide.structure.ObjectNotFoundException; 41 import org.apache.slide.structure.Structure; 42 import org.apache.slide.structure.SubjectNode; 43 44 47 public class AdminHelper { 48 49 private static final SlideToken ROOT = new SlideTokenImpl(new CredentialsToken("root")); 50 51 public static boolean login(NamespaceAccessToken nat, 52 String userId, 53 String password) throws Exception { 54 55 String usersPath = nat.getNamespaceConfig().getUsersPath(); 56 String userUri = usersPath + "/" + userId; 57 58 Content content = nat.getContentHelper(); 59 60 try { 61 NodeRevisionDescriptors revisions = content.retrieve(ROOT,userUri); 62 NodeRevisionDescriptor revision = content.retrieve(ROOT,revisions); 63 NodeProperty property = revision.getProperty( 64 "password",NodeProperty.SLIDE_NAMESPACE); 65 66 return property.getValue().equals(password); 67 } 68 catch (Exception e) { 69 e.printStackTrace(); 70 throw e; 71 } 72 } 73 74 public static void addUser(NamespaceAccessToken nat, 75 String caller, 76 String username, 77 String password) throws Exception { 78 79 String usersPath = nat.getNamespaceConfig().getUsersPath(); 80 String userUri = usersPath + "/" + username; 81 82 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 83 Structure structure = nat.getStructureHelper(); 84 Content content = nat.getContentHelper(); 85 86 try { 87 88 nat.begin(); 89 90 ObjectNode user = new SubjectNode(); 91 structure.create(slideToken,user,userUri); 92 93 NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor(); 95 descriptor.setCreationDate(new Date ()); 96 descriptor.setLastModified(new Date ()); 97 descriptor.setProperty(new NodeProperty( 98 "password",password,NodeProperty.SLIDE_NAMESPACE)); 99 content.create(slideToken,userUri,descriptor,null); 100 101 nat.commit(); 102 } 103 catch (Exception e) { 104 try { 105 nat.rollback(); 106 } 107 catch (Exception f) { 108 f.printStackTrace(); 109 } 110 throw e; 111 } 112 113 } 114 115 public static void addGroup(NamespaceAccessToken nat, 116 String caller, 117 String groupname) throws Exception { 118 119 String groupsPath = nat.getNamespaceConfig().getGroupsPath(); 120 String groupUri = groupsPath + "/" + groupname; 121 122 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 123 Structure structure = nat.getStructureHelper(); 124 Content content = nat.getContentHelper(); 125 126 try { 127 nat.begin(); 128 129 ObjectNode group = new SubjectNode(); 130 structure.create(slideToken,group,groupUri); 131 132 NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor(); 133 descriptor.setCreationDate(new Date ()); 134 descriptor.setLastModified(new Date ()); 135 136 content.create(slideToken,groupUri,descriptor,null); 137 138 nat.commit(); 139 } 140 catch (Exception e) { 141 try { 142 nat.rollback(); 143 } 144 catch (Exception f) { 145 f.printStackTrace(); 146 } 147 throw e; 148 } 149 150 } 151 152 public static void addRole(NamespaceAccessToken nat, 153 String caller, 154 String rolename) throws Exception { 155 156 String rolesPath = nat.getNamespaceConfig().getRolesPath(); 157 String roleUri = rolesPath + "/" + rolename; 158 159 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 160 Structure structure = nat.getStructureHelper(); 161 Content content = nat.getContentHelper(); 162 163 try { 164 nat.begin(); 165 166 ObjectNode role = new SubjectNode(); 167 structure.create(slideToken,role,roleUri); 168 169 NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor(); 170 descriptor.setCreationDate(new Date ()); 171 descriptor.setLastModified(new Date ()); 172 173 content.create(slideToken,roleUri,descriptor,null); 174 175 nat.commit(); 176 } 177 catch (Exception e) { 178 try { 179 nat.rollback(); 180 } 181 catch (Exception f) { 182 f.printStackTrace(); 183 } 184 throw e; 185 } 186 187 } 188 189 public static void removeObject(NamespaceAccessToken nat, 190 String caller, 191 String objectUri) throws Exception { 192 193 String usersPath = nat.getNamespaceConfig().getUsersPath(); 194 String callerUri = usersPath + "/" + caller; 195 196 if (callerUri.equals(objectUri)) { 198 return; 199 } 200 201 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 202 Macro macro = nat.getMacroHelper(); 203 204 try { 205 nat.begin(); 206 207 boolean recursive = true; 208 boolean overwrite = false; 209 MacroParameters parameters = new MacroParameters(recursive,overwrite); 210 211 macro.delete(slideToken,objectUri,parameters); 212 213 nat.commit(); 214 } 215 catch (Exception e) { 216 try { 217 nat.rollback(); 218 } 219 catch (Exception f) { 220 f.printStackTrace(); 221 } 222 throw e; 223 } 224 } 225 226 public static void addMember(NamespaceAccessToken nat, 227 String caller, 228 String objectUri, 229 String subjectUri) throws Exception { 230 231 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 232 Structure structure = nat.getStructureHelper(); 233 Content content = nat.getContentHelper(); 234 235 try { 236 237 structure.retrieve(slideToken,subjectUri); 239 240 NodeRevisionDescriptors descriptors = content.retrieve(slideToken,objectUri); 241 NodeRevisionDescriptor descriptor = content.retrieve(slideToken,descriptors); 242 NodeProperty property = descriptor.getProperty("group-member-set","DAV:"); 243 244 String value = null; 245 if (property != null) { 246 value = (String ) property.getValue(); 247 if (value.indexOf(subjectUri) != -1) { 248 return; 250 } 251 } 252 else { 253 value = ""; 254 } 255 value = value + "<D:href xmlns:D='DAV:'>" + subjectUri + "</D:href>"; 256 257 descriptor.setProperty("group-member-set","DAV:",value); 258 nat.begin(); 259 content.store(slideToken,objectUri,descriptor,null); 260 nat.commit(); 261 } 262 catch (ObjectNotFoundException e) { 263 } 265 catch (Exception e) { 266 try { 267 nat.rollback(); 268 } 269 catch (Exception f) { 270 f.printStackTrace(); 271 } 272 throw e; 273 } 274 } 275 276 public static void removeMember(NamespaceAccessToken nat, 277 String caller, 278 String objectUri, 279 String subjectUri) throws Exception { 280 281 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 282 Content content = nat.getContentHelper(); 283 284 try { 285 286 NodeRevisionDescriptors revisions = content.retrieve(slideToken,objectUri); 287 NodeRevisionDescriptor revision = content.retrieve(slideToken,revisions); 288 NodeProperty property = revision.getProperty("group-member-set","DAV:"); 289 290 if (property == null) { 291 return; 293 } 294 String value = (String ) property.getValue(); 295 296 int index = value.indexOf(subjectUri); 297 if (index == -1) { 298 return; 300 } 301 302 int end = index + subjectUri.length(); 304 do { 305 end++; 306 } 307 while (value.charAt(end) != '>'); 308 309 int from = index; 311 do { 312 from--; 313 } 314 while(value.charAt(from) != '<'); 315 316 String before = value.substring(0,from); 318 String after = value.substring(end+1); 319 value = before + after; 320 321 revision.setProperty("group-member-set","DAV:",value); 322 nat.begin(); 323 content.store(slideToken,objectUri,revision,null); 324 nat.commit(); 325 } 326 catch (ObjectNotFoundException e) { 327 } 329 catch (Exception e) { 330 try { 331 nat.rollback(); 332 } 333 catch (Exception f) { 334 f.printStackTrace(); 335 } 336 throw e; 337 } 338 } 339 340 public static void changePassword(NamespaceAccessToken nat, 341 String caller, 342 String userUri, 343 String password) throws Exception { 344 345 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 346 Content content = nat.getContentHelper(); 347 348 try { 349 nat.begin(); 350 351 NodeRevisionDescriptors revisions = content.retrieve(slideToken, userUri); 352 NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions); 353 revision.setLastModified(new Date ()); 354 revision.setProperty(new NodeProperty("password", password, NodeProperty.SLIDE_NAMESPACE)); 355 content.store(slideToken, userUri, revision, null); 356 357 nat.commit(); 358 } 359 catch (Exception e) { 360 try { 361 nat.rollback(); 362 } 363 catch (Exception f) { 364 f.printStackTrace(); 365 } 366 throw e; 367 } 368 } 369 370 public static List listPermissions(NamespaceAccessToken nat, 371 String caller, 372 String path) throws Exception { 373 374 String uri = getUriFromPath(nat,path); 375 376 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 377 Security security = nat.getSecurityHelper(); 378 379 List result = new ArrayList (); 380 try { 381 nat.begin(); 382 Enumeration permissions = security.enumeratePermissions(slideToken,uri,false); 383 while (permissions.hasMoreElements()) { 384 result.add(permissions.nextElement()); 385 } 386 nat.commit(); 387 return result; 388 } 389 catch (Exception e) { 390 try { 391 nat.rollback(); 392 } 393 catch (Exception f) { 394 f.printStackTrace(); 395 } 396 throw e; 397 } 398 } 399 400 public static List listLocks(NamespaceAccessToken nat, 401 String caller, 402 String path) throws Exception { 403 404 String uri = getUriFromPath(nat,path); 405 406 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 407 Lock lock = nat.getLockHelper(); 408 409 List result = new ArrayList (); 410 try { 411 nat.begin(); 412 Enumeration locks = lock.enumerateLocks(slideToken,uri,false); 413 while(locks.hasMoreElements()) { 414 result.add(locks.nextElement()); 415 } 416 nat.commit(); 417 return result; 418 } catch (Exception e) { 419 try { 420 nat.rollback(); 421 } 422 catch (Exception f) { 423 f.printStackTrace(); 424 } 425 throw e; 426 } 427 } 428 429 public static List listGroups(NamespaceAccessToken nat, String caller, String path) throws Exception { 430 List result = new ArrayList (); 431 432 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 433 Structure structure = nat.getStructureHelper(); 434 Content content = nat.getContentHelper(); 435 436 ObjectNode object = structure.retrieve(slideToken,path); 437 Enumeration enumeration = structure.getChildren(slideToken,object); 438 while (enumeration.hasMoreElements()) { 439 String uri = ((ObjectNode) enumeration.nextElement()).getUri(); 440 NodeRevisionDescriptors revisions = content.retrieve(slideToken, uri); 441 NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions); 442 NodeProperty property = revision.getProperty("group-member-set","DAV:"); 443 List members; 444 if (property != null) { 445 String value = (String ) property.getValue(); 446 members = new ArrayList (10); 447 int start = value.indexOf('>'), end = 0; 448 while (start != -1) { 449 end = value.indexOf('<',start); 450 if (end != -1) { 451 members.add(value.substring(start+1,end)); 452 } 453 end = value.indexOf('>',start+1); 454 start = value.indexOf('>',end+1); 455 } 456 } 457 else { 458 members = Collections.EMPTY_LIST; 459 } 460 result.add(new Group(uri,members)); 461 } 462 463 return result; 464 } 465 466 public static List listUsers(NamespaceAccessToken nat, 467 String caller) throws Exception { 468 return listObjects(nat,caller,nat.getNamespaceConfig().getUsersPath()); 469 } 470 471 public static List listPrivileges(NamespaceAccessToken nat, 472 String caller) throws Exception { 473 return listObjects(nat,caller,nat.getNamespaceConfig().getActionsPath()); 474 } 475 476 private static List listObjects(NamespaceAccessToken nat, 477 String caller, 478 String path) throws Exception { 479 480 List result = new ArrayList (); 481 482 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 483 Structure structure = nat.getStructureHelper(); 484 485 ObjectNode object = structure.retrieve(slideToken,path); 486 Enumeration enumeration = structure.getChildren(slideToken,object); 487 while (enumeration.hasMoreElements()) { 488 result.add(((ObjectNode) enumeration.nextElement()).getUri()); 489 } 490 491 return result; 492 } 493 494 public static void removePermission(NamespaceAccessToken nat, 495 String caller, 496 String path, 497 String subject, 498 String action) throws Exception { 499 500 String uri = getUriFromPath(nat,path); 501 502 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 503 Security security = nat.getSecurityHelper(); 504 505 try { 506 NodePermission permission = new NodePermission(uri, subject, action); 507 nat.begin(); 508 security.revokePermission(slideToken, permission); 509 nat.commit(); 510 } 511 catch (Exception e) { 512 try { 513 nat.rollback(); 514 } 515 catch (Exception f) { 516 f.printStackTrace(); 517 } 518 throw e; 519 } 520 521 } 522 523 public static void addPermission(NamespaceAccessToken nat, 524 String caller, 525 String path, 526 String subject, 527 String action, 528 String inheritable, 529 String negative) throws Exception { 530 531 String uri = getUriFromPath(nat,path); 532 533 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 534 Security security = nat.getSecurityHelper(); 535 536 boolean isInheritable = Boolean.valueOf(inheritable).booleanValue(); 537 boolean isNegative = Boolean.valueOf(negative).booleanValue(); 538 539 try { 540 NodePermission permission = new NodePermission(uri,subject,action,isInheritable,isNegative); 541 542 nat.begin(); 543 if (isNegative) { 544 security.denyPermission(slideToken,permission); 545 } 546 else { 547 security.grantPermission(slideToken,permission); 548 } 549 nat.commit(); 550 } catch (Exception e) { 551 try { 552 nat.rollback(); 553 } 554 catch (Exception f) { 555 f.printStackTrace(); 556 } 557 throw e; 558 } 559 } 560 561 public static void removeLock(NamespaceAccessToken nat, 562 String caller, 563 String uri, 564 String lockId) throws Exception { 565 566 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 567 Lock lock = nat.getLockHelper(); 568 569 try { 570 nat.begin(); 571 lock.unlock(slideToken,uri,lockId); 572 nat.commit(); 573 } 574 catch (Exception e) { 575 try { 576 nat.rollback(); 577 } 578 catch (Exception f) { 579 f.printStackTrace(); 580 } 581 throw e; 582 } 583 } 584 585 public static void addLock(NamespaceAccessToken nat, 586 String caller, 587 String path, 588 String subject, 589 String type, 590 String expiration, 591 String exclusive, 592 String inherit) throws Exception { 593 594 String uri = getUriFromPath(nat,path); 595 boolean isExclusive = Boolean.valueOf(exclusive).booleanValue(); 596 boolean isInherit = Boolean.valueOf(inherit).booleanValue(); 597 598 int intExpiration = Integer.valueOf(expiration).intValue(); 600 Date expire = new Date (System.currentTimeMillis() + intExpiration*1000*60); 601 602 SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller)); 603 Lock lock = nat.getLockHelper(); 604 605 try { 606 nat.begin(); 607 lock.lock(slideToken, new NodeLock(uri, subject, type, expire, isInherit, isExclusive, uri)); 608 nat.commit(); 609 } 610 catch (Exception e) { 611 try { 612 nat.rollback(); 613 } 614 catch (Exception f) { 615 f.printStackTrace(); 616 } 617 throw e; 618 } 619 } 620 621 private static String getUriFromPath(NamespaceAccessToken nat, 622 String path) { 623 String filesPath = nat.getNamespaceConfig().getFilesPath(); 624 String uri; 625 if (path.equals("/") || path.length() == 0) { 626 uri = filesPath; 627 } 628 else { 629 uri = filesPath + "/" + path; 630 } 631 return uri; 632 } 633 634 public static class Group { 635 private final String m_uri; 636 private final List m_members; 637 638 private Group(String uri, List members) { 639 m_uri = uri; 640 m_members = members; 641 } 642 643 public String getUri() { 644 return m_uri; 645 } 646 647 public List getMembers() { 648 return m_members; 649 } 650 651 public String toString() { 652 return m_uri; 653 } 654 } 655 } 656 | Popular Tags |