From: Jean-Marc V. <jm...@us...> - 2004-07-09 17:27:29
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/collections In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20007/src/org/exist/collections Modified Files: Collection.java Log Message: Refactoring: extract method: checkPermissions(DBBroker broker, String name) Index: Collection.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/collections/Collection.java,v retrieving revision 1.35 retrieving revision 1.36 diff -C2 -d -r1.35 -r1.36 *** Collection.java 9 Jul 2004 09:19:32 -0000 1.35 --- Collection.java 9 Jul 2004 17:27:20 -0000 1.36 *************** *** 714,746 **** InputSource source; try { ! lock.acquire(Lock.WRITE_LOCK); ! if (hasDocument(name) && (oldDoc = getDocument(broker, name)) != null) { ! if(oldDoc.isLockedForWrite()) ! throw new PermissionDeniedException("Document " + name + ! " is locked for write"); ! // check if the document is locked by another user ! User lockUser = oldDoc.getUserLock(); ! if(lockUser != null && !lockUser.equals(broker.getUser())) ! throw new PermissionDeniedException("The document is locked by user " + ! lockUser.getName()); ! ! // check if the document is currently being changed by someone else ! Lock oldLock = oldDoc.getUpdateLock(); ! oldLock.acquire(Lock.WRITE_LOCK); ! ! // do we have permissions for update? ! if (!oldDoc.getPermissions().validate(broker.getUser(), ! Permission.UPDATE)) ! throw new PermissionDeniedException( ! "Document \""+name+"\" exists and update is not allowed"); ! if (!(getPermissions().validate(broker.getUser(), Permission.UPDATE) || ! getPermissions().validate(broker.getUser(), Permission.WRITE))) ! throw new PermissionDeniedException( ! "Document exists and update is not allowed for the collection"); ! // do we have write permissions? ! } else if (!getPermissions().validate(broker.getUser(), ! Permission.WRITE)) ! throw new PermissionDeniedException( ! "Not allowed to write to collection " + getName()); document = new DocumentImpl(broker, name, this); --- 714,718 ---- InputSource source; try { ! oldDoc = checkPermissions(broker, name); document = new DocumentImpl(broker, name, this); *************** *** 880,884 **** } ! /** If an old document exists, keep information about the document * @param broker * @param name --- 852,856 ---- } ! /** If an old document exists, keep information about the document. * @param broker * @param name *************** *** 900,903 **** --- 872,919 ---- } + /** Check Permissions about user and document, and throw exceptions if necessary. + * @param broker + * @param name + * @return + * @throws LockException + * @throws PermissionDeniedException + */ + private DocumentImpl checkPermissions(DBBroker broker, String name) throws LockException, PermissionDeniedException { + DocumentImpl oldDoc = null; + lock.acquire(Lock.WRITE_LOCK); + if (hasDocument(name) && (oldDoc = getDocument(broker, name)) != null) { + + // jmv: Note: this was only in addDocument(DBBroker broker, String name, String data,) + if(oldDoc.isLockedForWrite()) + throw new PermissionDeniedException("Document " + name + + " is locked for write"); + + // check if the document is locked by another user + User lockUser = oldDoc.getUserLock(); + if(lockUser != null && !lockUser.equals(broker.getUser())) + throw new PermissionDeniedException("The document is locked by user " + + lockUser.getName()); + + // check if the document is currently being changed by someone else + Lock oldLock = oldDoc.getUpdateLock(); + oldLock.acquire(Lock.WRITE_LOCK); + + // do we have permissions for update? + if (!oldDoc.getPermissions().validate(broker.getUser(), + Permission.UPDATE)) + throw new PermissionDeniedException( + "Document exists and update is not allowed"); + if (!(getPermissions().validate(broker.getUser(), Permission.UPDATE) || + getPermissions().validate(broker.getUser(), Permission.WRITE))) + throw new PermissionDeniedException( + "Document exists and update is not allowed for the collection"); + // do we have write permissions? + } else if (!getPermissions().validate(broker.getUser(), + Permission.WRITE)) + throw new PermissionDeniedException( + "Not allowed to write to collection " + getName()); + return oldDoc; + } + public DocumentImpl addDocument(DBBroker broker, String name, InputSource source) throws EXistException, LockException, *************** *** 915,944 **** XMLReader reader; try { ! lock.acquire(Lock.WRITE_LOCK); ! if (hasDocument(name) && (oldDoc = getDocument(broker, name)) != null) { ! // check if the document is locked by another user ! User lockUser = oldDoc.getUserLock(); ! if(lockUser != null && !lockUser.equals(broker.getUser())) ! throw new PermissionDeniedException("The document is locked by user " + ! lockUser.getName()); ! ! // check if the document is currently being changed by someone else ! Lock oldLock = oldDoc.getUpdateLock(); ! oldLock.acquire(Lock.WRITE_LOCK); ! ! // do we have permissions for update? ! if (!oldDoc.getPermissions().validate(broker.getUser(), ! Permission.UPDATE)) ! throw new PermissionDeniedException( ! "Document exists and update is not allowed"); ! if (!(getPermissions().validate(broker.getUser(), Permission.UPDATE) || ! getPermissions().validate(broker.getUser(), Permission.WRITE))) ! throw new PermissionDeniedException( ! "Document exists and update is not allowed for the collection"); ! // do we have write permissions? ! } else if (!getPermissions().validate(broker.getUser(), ! Permission.WRITE)) ! throw new PermissionDeniedException( ! "Not allowed to write to collection " + getName()); document = new DocumentImpl(broker, name, this); --- 931,935 ---- XMLReader reader; try { ! oldDoc = checkPermissions(broker, name); document = new DocumentImpl(broker, name, this); *************** *** 1008,1012 **** throw new EXistException(e); } ! document.setMaxDepth(document.getMaxDepth() + 1); document.calculateTreeLevelStartPoints(); // new document is valid: remove old document --- 999,1003 ---- throw new EXistException(e); } ! document.setMaxDepth(document.getMaxDepth() + 1);//ddddddddddddddddddddddddddddddd document.calculateTreeLevelStartPoints(); // new document is valid: remove old document *************** *** 1103,1131 **** DOMStreamer streamer; try { ! lock.acquire(Lock.WRITE_LOCK); ! if (hasDocument(name) && (oldDoc = getDocument(broker, name)) != null) { ! // check if the document is locked by another user ! User lockUser = oldDoc.getUserLock(); ! if(lockUser != null && !lockUser.equals(broker.getUser())) ! throw new PermissionDeniedException("The document is locked by user " + ! lockUser.getName()); ! ! // check if the document is currently being changed by someone else ! oldDoc.getUpdateLock().acquire(Lock.WRITE_LOCK); ! ! // do we have permissions for update? ! if (!oldDoc.getPermissions().validate(broker.getUser(), ! Permission.UPDATE)) ! throw new PermissionDeniedException( ! "document exists and update " + "is not allowed"); ! if (!(getPermissions().validate(broker.getUser(), Permission.UPDATE) || ! getPermissions().validate(broker.getUser(), Permission.WRITE))) ! throw new PermissionDeniedException( ! "Document exists and update is not allowed for the collection"); ! // no: do we have write permissions? ! } else if (!getPermissions().validate(broker.getUser(), ! Permission.WRITE)) ! throw new PermissionDeniedException( ! "not allowed to write to collection " + getName()); document = new DocumentImpl(broker, name, this); --- 1094,1099 ---- DOMStreamer streamer; try { ! ! oldDoc = checkPermissions(broker, name); document = new DocumentImpl(broker, name, this); *************** *** 1249,1291 **** BinaryDocument blob = null; try { ! lock.acquire(Lock.WRITE_LOCK); ! DocumentImpl oldDoc = getDocument(broker, name); ! if (oldDoc != null) { ! if(oldDoc.isLockedForWrite()) ! throw new PermissionDeniedException("Document " + name + ! " is already locked for write"); ! // check if the document is locked by another user ! User lockUser = oldDoc.getUserLock(); ! if(lockUser != null && !lockUser.equals(broker.getUser())) ! throw new PermissionDeniedException("The document is locked by user " + ! lockUser.getName()); ! // do we have permissions for update? ! if (!oldDoc.getPermissions().validate(broker.getUser(), ! Permission.UPDATE)) ! throw new PermissionDeniedException( ! "document exists and update is not allowed"); ! // no: do we have write permissions? ! } else if (!getPermissions().validate(broker.getUser(), ! Permission.WRITE)) ! throw new PermissionDeniedException( ! "not allowed to write to collection " + getName()); blob = new BinaryDocument(broker, name, this); - // if (oldDoc != null) { - // blob.setCreated(oldDoc.getCreated()); - // blob.setLastModified(System.currentTimeMillis()); - // blob.setPermissions(oldDoc.getPermissions()); - // - // LOG.debug("removing old document " + oldDoc.getFileName()); - // if (oldDoc instanceof BinaryDocument) - // broker.removeBinaryResource((BinaryDocument) oldDoc); - // else - // broker.removeDocument(getName() + '/' + oldDoc.getFileName()); - // } else { - // blob.setCreated(System.currentTimeMillis()); - // blob.getPermissions().setOwner(broker.getUser()); - // blob.getPermissions().setGroup( - // broker.getUser().getPrimaryGroup()); - // } manageDocumentInformation(broker, name, oldDoc, blob ); --- 1217,1224 ---- BinaryDocument blob = null; try { ! ! DocumentImpl oldDoc = checkPermissions(broker, name); blob = new BinaryDocument(broker, name, this); manageDocumentInformation(broker, name, oldDoc, blob ); |