Skip to content

Commit

Permalink
fixup! Sharing link & mail parity
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 24, 2020
1 parent 1d8239c commit 2dbe01b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 71 deletions.
32 changes: 0 additions & 32 deletions apps/files_sharing/src/components/SharingEntryLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@

<script>
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import Vue from 'vue'

import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
Expand All @@ -338,8 +337,6 @@ import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import Share from '../models/Share'
import SharesMixin from '../mixins/SharesMixin'

const passwordSet = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789'

export default {
name: 'SharingEntryLink',

Expand Down Expand Up @@ -760,35 +757,6 @@ export default {
this.queueUpdate('label')
}
},

/**
* Generate a valid policy password or
* request a valid password if password_policy
* is enabled
*
* @returns {string} a valid password
*/
async generatePassword() {
// password policy is enabled, let's request a pass
if (this.config.passwordPolicy.api && this.config.passwordPolicy.api.generate) {
try {
const request = await axios.get(this.config.passwordPolicy.api.generate)
if (request.data.ocs.data.password) {
return request.data.ocs.data.password
}
} catch (error) {
console.info('Error generating password from password_policy', error)
}
}

// generate password of 10 length based on passwordSet
return Array(10).fill(0)
.reduce((prev, curr) => {
prev += passwordSet.charAt(Math.floor(Math.random() * passwordSet.length))
return prev
}, '')
},

async copyLink() {
try {
await this.$copyText(this.shareLink)
Expand Down
31 changes: 31 additions & 0 deletions apps/files_sharing/src/mixins/SharesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*
*/

import axios from '@nextcloud/axios'
import PQueue from 'p-queue'
import debounce from 'debounce'

Expand All @@ -29,6 +30,8 @@ import ShareTypes from './ShareTypes'
import Config from '../services/ConfigService'
import { getCurrentUser } from '@nextcloud/auth'

const passwordSet = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789'

export default {
mixins: [SharesRequests, ShareTypes],

Expand Down Expand Up @@ -323,5 +326,33 @@ export default {
return (this.dateTomorrow && dateMoment.isBefore(this.dateTomorrow, 'day'))
|| (this.dateMaxEnforced && dateMoment.isSameOrAfter(this.dateMaxEnforced, 'day'))
},

/**
* Generate a valid policy password or
* request a valid password if password_policy
* is enabled
*
* @returns {string} a valid password
*/
async generatePassword() {
// password policy is enabled, let's request a pass
if (this.config.passwordPolicy.api && this.config.passwordPolicy.api.generate) {
try {
const request = await axios.get(this.config.passwordPolicy.api.generate)
if (request.data.ocs.data.password) {
return request.data.ocs.data.password
}
} catch (error) {
console.info('Error generating password from password_policy', error)
}
}

// generate password of 10 length based on passwordSet
return Array(10).fill(0)
.reduce((prev, curr) => {
prev += passwordSet.charAt(Math.floor(Math.random() * passwordSet.length))
return prev
}, '')
},
},
}
10 changes: 6 additions & 4 deletions apps/sharebymail/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@

use OCA\ShareByMail\Settings\SettingsManager;
use OCP\Capabilities\ICapability;
use OCP\Share\IManager;

class Capabilities implements ICapability {

/** @var SettingsManager */
/** @var IManager */
private $manager;

public function __construct(SettingsManager $manager) {
public function __construct(IManager $manager) {
$this->manager = $manager;
}

Expand All @@ -45,16 +46,17 @@ public function getCapabilities(): array {
[
'sharebymail' =>
[
'enabled' => true,
'enabled' => $this->manager->shareApiAllowLinks(),
'upload_files_drop' => [
'enabled' => true,
],
'password' => [
'enabled' => true,
'enforced' => $this->manager->enforcePasswordProtection(),
'enforced' => $this->manager->shareApiLinkEnforcePassword(),
],
'expire_date' => [
'enabled' => true,
'enforced' => $this->manager->shareApiLinkDefaultExpireDateEnforced(),
],
]
]
Expand Down
12 changes: 0 additions & 12 deletions apps/sharebymail/lib/Settings/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class SettingsManager {

private $sendPasswordByMailDefault = 'yes';

private $enforcePasswordProtectionDefault = 'no';

public function __construct(IConfig $config) {
$this->config = $config;
}
Expand All @@ -51,14 +49,4 @@ public function sendPasswordByMail(): bool {
$sendPasswordByMail = $this->config->getAppValue('sharebymail', 'sendpasswordmail', $this->sendPasswordByMailDefault);
return $sendPasswordByMail === 'yes';
}

/**
* do we require a share by mail to be password protected
*
* @return bool
*/
public function enforcePasswordProtection(): bool {
$enforcePassword = $this->config->getAppValue('sharebymail', 'enforcePasswordProtection', $this->enforcePasswordProtectionDefault);
return $enforcePassword === 'yes';
}
}
36 changes: 20 additions & 16 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;

Expand Down Expand Up @@ -110,6 +111,9 @@ class ShareByMailProvider implements IShareProvider {
/** @var IEventDispatcher */
private $eventDispatcher;

/** @var IShareManager */
private $shareManager;

/**
* Return the identifier of this provider.
*
Expand All @@ -119,21 +123,20 @@ public function identifier() {
return 'ocMailShare';
}

public function __construct(
IDBConnection $connection,
ISecureRandom $secureRandom,
IUserManager $userManager,
IRootFolder $rootFolder,
IL10N $l,
ILogger $logger,
IMailer $mailer,
IURLGenerator $urlGenerator,
IManager $activityManager,
SettingsManager $settingsManager,
Defaults $defaults,
IHasher $hasher,
IEventDispatcher $eventDispatcher
) {
public function __construct(IDBConnection $connection,
ISecureRandom $secureRandom,
IUserManager $userManager,
IRootFolder $rootFolder,
IL10N $l,
ILogger $logger,
IMailer $mailer,
IURLGenerator $urlGenerator,
IManager $activityManager,
SettingsManager $settingsManager,
Defaults $defaults,
IHasher $hasher,
IEventDispatcher $eventDispatcher,
IShareManager $shareManager) {
$this->dbConnection = $connection;
$this->secureRandom = $secureRandom;
$this->userManager = $userManager;
Expand All @@ -147,6 +150,7 @@ public function __construct(
$this->defaults = $defaults;
$this->hasher = $hasher;
$this->eventDispatcher = $eventDispatcher;
$this->shareManager = $shareManager;
}

/**
Expand All @@ -173,7 +177,7 @@ public function create(IShare $share) {
// if the admin enforces a password for all mail shares we create a
// random password and send it to the recipient
$password = $share->getPassword() ?: '';
$passwordEnforced = $this->settingsManager->enforcePasswordProtection();
$passwordEnforced = $this->shareManager->shareApiLinkEnforcePassword();
if ($passwordEnforced && empty($password)) {
$password = $this->autoGeneratePassword($share);
}
Expand Down
4 changes: 0 additions & 4 deletions apps/sharebymail/templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
p('checked');
} ?> />
<label for="sendPasswordMail"><?php p($l->t('Send password by mail')); ?></label><br/>
<input id="enforcePasswordProtection" type="checkbox" class="checkbox" <?php if ($_['enforcePasswordProtection']) {
p('checked');
} ?> />
<label for="enforcePasswordProtection"><?php p($l->t('Enforce password protection')); ?></label>
</p>

</div>
8 changes: 6 additions & 2 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,15 @@ protected function generalCreateChecks(IShare $share) {
if (!$this->groupManager->groupExists($share->getSharedWith())) {
throw new \InvalidArgumentException('SharedWith is not a valid group');
}
} elseif ($share->getShareType() === IShare::TYPE_LINK
|| $share->getShareType() === IShare::TYPE_EMAIL) {
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
// No check for TYPE_EMAIL here as we have a recipient for them
if ($share->getSharedWith() !== null) {
throw new \InvalidArgumentException('SharedWith should be empty');
}
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
if ($share->getSharedWith() === null) {
throw new \InvalidArgumentException('SharedWith should not be empty');
}
} elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
if ($share->getSharedWith() === null) {
throw new \InvalidArgumentException('SharedWith should not be empty');
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OCP\IServerContainer;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
use OCP\Share\IManager;

/**
* Class ProviderFactory
Expand Down Expand Up @@ -185,7 +186,8 @@ protected function getShareByMailProvider() {
$settingsManager,
$this->serverContainer->query(Defaults::class),
$this->serverContainer->getHasher(),
$this->serverContainer->get(IEventDispatcher::class)
$this->serverContainer->get(IEventDispatcher::class),
$this->serverContainer->get(IManager::class)
);
}

Expand Down

0 comments on commit 2dbe01b

Please sign in to comment.