6363use OCP \Security \IHasher ;
6464use OCP \Security \ISecureRandom ;
6565use OCP \Share ;
66+ use OCP \Share \Exceptions \AlreadySharedException ;
6667use OCP \Share \Exceptions \GenericShareException ;
6768use OCP \Share \Exceptions \ShareNotFound ;
6869use OCP \Share \IManager ;
@@ -564,7 +565,7 @@ protected function userCreateChecks(IShare $share) {
564565
565566 // Identical share already existst
566567 if ($ existingShare ->getSharedWith () === $ share ->getSharedWith () && $ existingShare ->getShareType () === $ share ->getShareType ()) {
567- throw new \ Exception ('Path is already shared with this user ' );
568+ throw new AlreadySharedException ('Path is already shared with this user ' , $ existingShare );
568569 }
569570
570571 // The share is already shared with this user via a group share
@@ -574,7 +575,7 @@ protected function userCreateChecks(IShare $share) {
574575 $ user = $ this ->userManager ->get ($ share ->getSharedWith ());
575576
576577 if ($ group ->inGroup ($ user ) && $ existingShare ->getShareOwner () !== $ share ->getShareOwner ()) {
577- throw new \ Exception ('Path is already shared with this user ' );
578+ throw new AlreadySharedException ('Path is already shared with this user ' , $ existingShare );
578579 }
579580 }
580581 }
@@ -619,7 +620,7 @@ protected function groupCreateChecks(IShare $share) {
619620 }
620621
621622 if ($ existingShare ->getSharedWith () === $ share ->getSharedWith () && $ existingShare ->getShareType () === $ share ->getShareType ()) {
622- throw new \ Exception ('Path is already shared with this group ' );
623+ throw new AlreadySharedException ('Path is already shared with this group ' , $ existingShare );
623624 }
624625 }
625626 }
@@ -733,77 +734,82 @@ public function createShare(IShare $share) {
733734 }
734735 }
735736
736- //Verify share type
737- if ($ share ->getShareType () === IShare::TYPE_USER ) {
738- $ this ->userCreateChecks ($ share );
739-
740- //Verify the expiration date
741- $ share = $ this ->validateExpirationDateInternal ($ share );
742- } elseif ($ share ->getShareType () === IShare::TYPE_GROUP ) {
743- $ this ->groupCreateChecks ($ share );
737+ try {
738+ //Verify share type
739+ if ($ share ->getShareType () === IShare::TYPE_USER ) {
740+ $ this ->userCreateChecks ($ share );
744741
745- //Verify the expiration date
746- $ share = $ this ->validateExpirationDateInternal ($ share );
747- } elseif ($ share ->getShareType () === IShare::TYPE_LINK ) {
748- $ this ->linkCreateChecks ($ share );
749- $ this ->setLinkParent ($ share );
742+ //Verify the expiration date
743+ $ share = $ this ->validateExpirationDateInternal ($ share );
744+ } elseif ($ share ->getShareType () === IShare::TYPE_GROUP ) {
745+ $ this ->groupCreateChecks ($ share );
750746
751- /*
752- * For now ignore a set token.
753- */
754- $ share ->setToken (
755- $ this ->secureRandom ->generate (
756- \OC \Share \Constants::TOKEN_LENGTH ,
757- \OCP \Security \ISecureRandom::CHAR_HUMAN_READABLE
758- )
759- );
747+ //Verify the expiration date
748+ $ share = $ this ->validateExpirationDateInternal ($ share );
749+ } elseif ($ share ->getShareType () === IShare::TYPE_LINK ) {
750+ $ this ->linkCreateChecks ($ share );
751+ $ this ->setLinkParent ($ share );
752+
753+ /*
754+ * For now ignore a set token.
755+ */
756+ $ share ->setToken (
757+ $ this ->secureRandom ->generate (
758+ \OC \Share \Constants::TOKEN_LENGTH ,
759+ \OCP \Security \ISecureRandom::CHAR_HUMAN_READABLE
760+ )
761+ );
760762
761- //Verify the expiration date
762- $ share = $ this ->validateExpirationDate ($ share );
763+ //Verify the expiration date
764+ $ share = $ this ->validateExpirationDate ($ share );
763765
764- //Verify the password
765- $ this ->verifyPassword ($ share ->getPassword ());
766+ //Verify the password
767+ $ this ->verifyPassword ($ share ->getPassword ());
766768
767- // If a password is set. Hash it!
768- if ($ share ->getPassword () !== null ) {
769- $ share ->setPassword ($ this ->hasher ->hash ($ share ->getPassword ()));
769+ // If a password is set. Hash it!
770+ if ($ share ->getPassword () !== null ) {
771+ $ share ->setPassword ($ this ->hasher ->hash ($ share ->getPassword ()));
772+ }
773+ } elseif ($ share ->getShareType () === IShare::TYPE_EMAIL ) {
774+ $ share ->setToken (
775+ $ this ->secureRandom ->generate (
776+ \OC \Share \Constants::TOKEN_LENGTH ,
777+ \OCP \Security \ISecureRandom::CHAR_HUMAN_READABLE
778+ )
779+ );
770780 }
771- } elseif ($ share ->getShareType () === IShare::TYPE_EMAIL ) {
772- $ share ->setToken (
773- $ this ->secureRandom ->generate (
774- \OC \Share \Constants::TOKEN_LENGTH ,
775- \OCP \Security \ISecureRandom::CHAR_HUMAN_READABLE
776- )
777- );
778- }
779781
780- // Cannot share with the owner
781- if ($ share ->getShareType () === IShare::TYPE_USER &&
782- $ share ->getSharedWith () === $ share ->getShareOwner ()) {
783- throw new \InvalidArgumentException ('Can’t share with the share owner ' );
784- }
782+ // Cannot share with the owner
783+ if ($ share ->getShareType () === IShare::TYPE_USER &&
784+ $ share ->getSharedWith () === $ share ->getShareOwner ()) {
785+ throw new \InvalidArgumentException ('Can’t share with the share owner ' );
786+ }
785787
786- // Generate the target
787- $ target = $ this ->config ->getSystemValue ('share_folder ' , '/ ' ) .'/ ' . $ share ->getNode ()->getName ();
788- $ target = \OC \Files \Filesystem::normalizePath ($ target );
789- $ share ->setTarget ($ target );
788+ // Generate the target
789+ $ target = $ this ->config ->getSystemValue ('share_folder ' , '/ ' ) . '/ ' . $ share ->getNode ()->getName ();
790+ $ target = \OC \Files \Filesystem::normalizePath ($ target );
791+ $ share ->setTarget ($ target );
790792
791- // Pre share event
792- $ event = new GenericEvent ($ share );
793- $ this ->legacyDispatcher ->dispatch ('OCP\Share::preShare ' , $ event );
794- if ($ event ->isPropagationStopped () && $ event ->hasArgument ('error ' )) {
795- throw new \Exception ($ event ->getArgument ('error ' ));
796- }
793+ // Pre share event
794+ $ event = new GenericEvent ($ share );
795+ $ this ->legacyDispatcher ->dispatch ('OCP\Share::preShare ' , $ event );
796+ if ($ event ->isPropagationStopped () && $ event ->hasArgument ('error ' )) {
797+ throw new \Exception ($ event ->getArgument ('error ' ));
798+ }
797799
798- $ oldShare = $ share ;
799- $ provider = $ this ->factory ->getProviderForType ($ share ->getShareType ());
800- $ share = $ provider ->create ($ share );
801- //reuse the node we already have
802- $ share ->setNode ($ oldShare ->getNode ());
800+ $ oldShare = $ share ;
801+ $ provider = $ this ->factory ->getProviderForType ($ share ->getShareType ());
802+ $ share = $ provider ->create ($ share );
803+ //reuse the node we already have
804+ $ share ->setNode ($ oldShare ->getNode ());
803805
804- // Reset the target if it is null for the new share
805- if ($ share ->getTarget () === '' ) {
806- $ share ->setTarget ($ target );
806+ // Reset the target if it is null for the new share
807+ if ($ share ->getTarget () === '' ) {
808+ $ share ->setTarget ($ target );
809+ }
810+ } catch (AlreadySharedException $ e ) {
811+ // if a share for the same target already exists, dont create a new one, but do trigger the hooks and notifications again
812+ $ share = $ e ->getExistingShare ();
807813 }
808814
809815 // Post share event
0 commit comments