1 40 package org.dspace.authorize; 41 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 import org.dspace.content.DSpaceObject; 48 import org.dspace.core.Constants; 49 import org.dspace.core.Context; 50 import org.dspace.eperson.EPerson; 51 import org.dspace.eperson.Group; 52 import org.dspace.storage.rdbms.DatabaseManager; 53 import org.dspace.storage.rdbms.TableRow; 54 import org.dspace.storage.rdbms.TableRowIterator; 55 56 71 public class AuthorizeManager 72 { 73 91 public static void authorizeAnyOf(Context c, DSpaceObject o, int[] actions) 92 throws AuthorizeException, SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 93 { 94 AuthorizeException ex = null; 95 96 for (int i = 0; i < actions.length; i++) 97 { 98 try 99 { 100 authorizeAction(c, o, actions[i]); 101 102 return; 103 } 104 catch (AuthorizeException e) 105 { 106 if (ex == null) 107 { 108 ex = e; 109 } 110 } 111 } 112 113 throw ex; 114 } 115 116 131 public static void authorizeAction(Context c, DSpaceObject o, int action) 132 throws AuthorizeException, SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 133 { 134 if (o == null) 135 { 136 String actionText; 138 139 if (action == -1) 140 { 141 actionText = "null"; 142 } 143 else 144 { 145 actionText = Constants.actionText[action]; 146 } 147 148 EPerson e = c.getCurrentUser(); 149 int userid; 150 151 if (e == null) 152 { 153 userid = 0; 154 } 155 else 156 { 157 userid = e.getID(); 158 } 159 160 throw new AuthorizeException( 161 "Authorization attempted on null DSpace object " 162 + actionText + " by user " + userid); 163 } 164 165 if (!authorize(c, o, action, c.getCurrentUser())) 166 { 167 int otype = o.getType(); 169 int oid = o.getID(); 170 int userid; 171 EPerson e = c.getCurrentUser(); 172 173 if (e == null) 174 { 175 userid = 0; 176 } 177 else 178 { 179 userid = e.getID(); 180 } 181 182 String actionText; 186 187 if (action == -1) 188 { 189 actionText = "null"; 190 } 191 else 192 { 193 actionText = Constants.actionText[action]; 194 } 195 196 throw new AuthorizeException("Authorization denied for action " 197 + actionText + " on " + Constants.typeText[otype] + ":" 198 + oid + " by user " + userid, o, action); 199 } 200 } 201 202 217 public static boolean authorizeActionBoolean(Context c, DSpaceObject o, 218 int a) throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 219 { 220 boolean isAuthorized = true; 221 222 if (o == null) 223 { 224 return false; 225 } 226 227 try 228 { 229 authorizeAction(c, o, a); 230 } 231 catch (AuthorizeException e) 232 { 233 isAuthorized = false; 234 } 235 236 return isAuthorized; 237 } 238 239 258 private static boolean authorize(Context c, DSpaceObject o, int action, 259 EPerson e) throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 260 { 261 int userid; 262 263 if (o == null) 265 { 266 return false; 267 } 268 269 if (c.ignoreAuthorization()) 271 { 272 return true; 273 } 274 275 if (e == null) 277 { 278 userid = 0; 279 } 280 else 281 { 282 userid = e.getID(); 283 284 if (isAdmin(c)) 287 { 288 return true; 289 } 290 } 291 292 List policies = getPoliciesActionFilter(c, o, action); 293 Iterator i = policies.iterator(); 294 295 while (i.hasNext()) 296 { 297 ResourcePolicy rp = (ResourcePolicy) i.next(); 298 299 if (rp.isDateValid()) 301 { 302 if ((rp.getEPersonID() != -1) && (rp.getEPersonID() == userid)) 303 { 304 return true; } 306 307 if ((rp.getGroupID() != -1) 308 && (Group.isMember(c, rp.getGroupID()))) 309 { 310 return true; 313 } 314 } 315 } 316 317 return false; 319 } 320 321 325 336 public static boolean isAdmin(Context c) throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 337 { 338 if (c.ignoreAuthorization()) 340 { 341 return true; 342 } 343 344 EPerson e = c.getCurrentUser(); 345 346 if (e == null) 347 { 348 return false; } 350 else 351 { 352 return Group.isMember(c, 1); 353 } 354 } 355 356 360 375 public static void addPolicy(Context c, DSpaceObject o, int actionID, 376 EPerson e) throws SQLException , AuthorizeException 377 { 378 ResourcePolicy rp = ResourcePolicy.create(c); 379 380 rp.setResource(o); 381 rp.setAction(actionID); 382 rp.setEPerson(e); 383 384 rp.update(); 385 } 386 387 403 public static void addPolicy(Context c, DSpaceObject o, int actionID, 404 Group g) throws SQLException , AuthorizeException 405 { 406 ResourcePolicy rp = ResourcePolicy.create(c); 407 408 rp.setResource(o); 409 rp.setAction(actionID); 410 rp.setGroup(g); 411 412 rp.update(); 413 } 414 415 423 public static List getPolicies(Context c, DSpaceObject o) 424 throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 425 { 426 TableRowIterator tri = DatabaseManager.queryTable(c, "resourcepolicy", 427 "SELECT * FROM resourcepolicy WHERE resource_type_id= ? AND resource_id= ? ", 428 o.getType(),o.getID()); 429 430 List policies = new ArrayList (); 431 432 while (tri.hasNext()) 433 { 434 TableRow row = tri.next(); 435 436 ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( 438 ResourcePolicy.class, row.getIntColumn("policy_id")); 439 440 if (cachepolicy != null) 441 { 442 policies.add(cachepolicy); 443 } 444 else 445 { 446 policies.add(new ResourcePolicy(c, row)); 447 } 448 } 449 tri.close(); 450 451 return policies; 452 } 453 454 466 public static List getPoliciesActionFilter(Context c, DSpaceObject o, 467 int actionID) throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 468 { 469 TableRowIterator tri = DatabaseManager.queryTable(c, "resourcepolicy", 470 "SELECT * FROM resourcepolicy WHERE resource_type_id= ? "+ 471 "AND resource_id= ? AND action_id= ? ", 472 o.getType(), o.getID(),actionID); 473 474 List policies = new ArrayList (); 475 476 while (tri.hasNext()) 477 { 478 TableRow row = tri.next(); 479 480 ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( 482 ResourcePolicy.class, row.getIntColumn("policy_id")); 483 484 if (cachepolicy != null) 485 { 486 policies.add(cachepolicy); 487 } 488 else 489 { 490 policies.add(new ResourcePolicy(c, row)); 491 } 492 } 493 tri.close(); 494 495 return policies; 496 } 497 498 511 public static void inheritPolicies(Context c, DSpaceObject src, 512 DSpaceObject dest) throws SQLException , AuthorizeException 513 { 514 List policies = getPolicies(c, src); 516 517 addPolicies(c, policies, dest); 518 } 519 520 534 public static void addPolicies(Context c, List policies, DSpaceObject dest) 535 throws SQLException , AuthorizeException 536 { 537 Iterator i = policies.iterator(); 538 539 while (i.hasNext()) 541 { 542 ResourcePolicy srp = (ResourcePolicy) i.next(); 543 544 ResourcePolicy drp = ResourcePolicy.create(c); 545 546 drp.setResource(dest); 548 drp.setAction(srp.getAction()); 549 drp.setEPerson(srp.getEPerson()); 550 drp.setGroup(srp.getGroup()); 551 drp.setStartDate(srp.getStartDate()); 552 drp.setEndDate(srp.getEndDate()); 553 554 drp.update(); 556 } 557 } 558 559 569 public static void removeAllPolicies(Context c, DSpaceObject o) 570 throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 571 { 572 DatabaseManager.updateQuery(c, "DELETE FROM resourcepolicy WHERE " 574 + "resource_type_id= ? AND resource_id= ? ", 575 o.getType(), o.getID()); 576 } 577 578 592 public static void removePoliciesActionFilter(Context context, 593 DSpaceObject dso, int actionID) throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 594 { 595 if (actionID == -1) 596 { 597 removeAllPolicies(context, dso); 599 } 600 else 601 { 602 DatabaseManager.updateQuery(context, 603 "DELETE FROM resourcepolicy WHERE resource_type_id= ? AND "+ 604 "resource_id= ? AND action_id= ? ", 605 dso.getType(), dso.getID(), actionID); 606 } 607 } 608 609 620 public static void removeGroupPolicies(Context c, int groupID) 621 throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 622 { 623 DatabaseManager.updateQuery(c, "DELETE FROM resourcepolicy WHERE " 624 + "epersongroup_id= ? ", groupID); 625 } 626 627 640 public static void removeGroupPolicies(Context c, DSpaceObject o, Group g) 641 throws SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 642 { 643 DatabaseManager.updateQuery(c, "DELETE FROM resourcepolicy WHERE " 644 + "resource_type_id= ? AND resource_id= ? AND epersongroup_id= ? ", 645 o.getType(), o.getID(), g.getID()); 646 } 647 648 663 public static Group[] getAuthorizedGroups(Context c, DSpaceObject o, 664 int actionID) throws java.sql.SQLException ![JavaDoc](../../../../cmn/javadoc.gif) 665 { 666 TableRowIterator tri = DatabaseManager.queryTable(c, "resourcepolicy", 668 "SELECT * FROM resourcepolicy WHERE resource_type_id= ? "+ 669 "AND resource_id= ? AND action_id= ? ",o.getType(),o.getID(),actionID); 670 671 List groups = new ArrayList (); 672 673 while (tri.hasNext()) 674 { 675 TableRow row = tri.next(); 676 677 ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( 679 ResourcePolicy.class, row.getIntColumn("policy_id")); 680 681 ResourcePolicy myPolicy = null; 682 683 if (cachepolicy != null) 684 { 685 myPolicy = cachepolicy; 686 } 687 else 688 { 689 myPolicy = new ResourcePolicy(c, row); 690 } 691 692 Group myGroup = myPolicy.getGroup(); 694 695 if (myGroup != null) 696 { 697 groups.add(myGroup); 698 } 699 } 700 tri.close(); 701 702 Group[] groupArray = new Group[groups.size()]; 703 groupArray = (Group[]) groups.toArray(groupArray); 704 705 return groupArray; 706 } 707 } 708 | Popular Tags |