<commit_before><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Helper\Sharing;

use OCA\Passwords\Db\Password;
use OCA\Passwords\Db\PasswordRevision;
use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Helper\Settings\ShareSettingsHelper;
use OCA\Passwords\Services\EncryptionService;
use OCA\Passwords\Services\Object\PasswordRevisionService;
use OCA\Passwords\Services\Object\PasswordService;
use OCA\Passwords\Services\Object\ShareService;

/**
 * Class CreatePasswordShareHelper
 *
 * @package OCA\Passwords\Helper\Share
 */
class CreatePasswordShareHelper {

    /**
     * @var ShareService
     */
    protected $modelService;

    /**
     * @var ShareSettingsHelper
     */
    protected $shareSettings;

    /**
     * @var PasswordService
     */
    protected $passwordModelService;

    /**
     * @var PasswordRevisionService
     */
    protected $passwordRevisionService;

    /**
     * CreatePasswordShareHelper constructor.
     *
     * @param ShareService            $modelService
     * @param ShareSettingsHelper     $shareSettings
     * @param PasswordService         $passwordModelService
     * @param PasswordRevisionService $passwordRevisionService
     */
    public function __construct(
        ShareService $modelService,
        ShareSettingsHelper $shareSettings,
        PasswordService $passwordModelService,
        PasswordRevisionService $passwordRevisionService
    ) {
        $this->modelService            = $modelService;
        $this->shareSettings           = $shareSettings;
        $this->passwordModelService    = $passwordModelService;
        $this->passwordRevisionService = $passwordRevisionService;
    }

    /**
     * @param string   $password
     * @param string   $receiver
     * @param string   $type
     * @param int|null $expires
     * @param bool     $editable
     * @param bool     $shareable
     *
     * @return \OCA\Passwords\Db\ModelInterface|\OCA\Passwords\Db\Share
     * @throws ApiException
     * @throws \Exception
     * @throws \OCP\AppFramework\Db\DoesNotExistException
     * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
     */
    public function createPasswordShare(
        string $password,
        string $receiver,
        string $type = 'user',
        int $expires = null,
        bool $editable = false,
        bool $shareable = false
    ) {
        $expires = $this->checkExpires($expires);
        $this->checkType($type);

        /** @var Password $model */
        $model = $this->passwordModelService->findByUuid($password);
        if($model->getShareId()) $editable = $this->checkSourceShare($receiver, $model);

        $this->checkIfAlreadyShared($receiver, $model);

        /** @var PasswordRevision $revision */
        $revision = $this->passwordRevisionService->findByUuid($model->getRevision(), true);
        $this->checkIfRevisionCanBeShared($revision);

        if($revision->getSseType() !== EncryptionService::SSE_ENCRYPTION_V1) {
            $this->downgradeSSE($revision, $model);
        }

        $share = $this->modelService->create($model->getUuid(), $receiver, $type, $editable, $expires, $shareable);
        $this->modelService->save($share);

        $this->setHasSharesFlag($model);

        return $share;
    }

    /**
     * @param int|null $expires
     *
     * @return int|null
     * @throws ApiException
     */
    protected function checkExpires(?int $expires) {
        if(empty($expires)) $expires = null;
        if($expires !== null && $expires < time()) throw new ApiException('Invalid expiration date', 400);

        return $expires;
    }

    /**
     * @param string $type
     *
     * @throws ApiException
     */
    protected function checkType(string $type): void {
        if($type !== 'user') throw new ApiException('Invalid share type', 400);
    }

    /**
     * @param string $receiver
     * @param        $model
     *
     * @return bool
     * @throws ApiException
     * @throws \OCP\AppFramework\Db\DoesNotExistException
     * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
     */
    protected function checkSourceShare(string $receiver, Password $model): bool {
        $sourceShare = $this->modelService->findByUuid($model->getShareId());
        if($sourceShare->getUserId() === $receiver) throw new ApiException('Invalid receiver uid', 400);
        if(!$sourceShare->isShareable() || !$this->shareSettings->get('resharing')) throw new ApiException('Entity not shareable', 403);
        if(!$sourceShare->isEditable()) $editable = false;

        return $editable;<commit_msg>Fix editable check in sharing<commit_after><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Helper\Sharing;

use OCA\Passwords\Db\Password;
use OCA\Passwords\Db\PasswordRevision;
use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Helper\Settings\ShareSettingsHelper;
use OCA\Passwords\Services\EncryptionService;
use OCA\Passwords\Services\Object\PasswordRevisionService;
use OCA\Passwords\Services\Object\PasswordService;
use OCA\Passwords\Services\Object\ShareService;

/**
 * Class CreatePasswordShareHelper
 *
 * @package OCA\Passwords\Helper\Share
 */
class CreatePasswordShareHelper {

    /**
     * @var ShareService
     */
    protected $modelService;

    /**
     * @var ShareSettingsHelper
     */
    protected $shareSettings;

    /**
     * @var PasswordService
     */
    protected $passwordModelService;

    /**
     * @var PasswordRevisionService
     */
    protected $passwordRevisionService;

    /**
     * CreatePasswordShareHelper constructor.
     *
     * @param ShareService            $modelService
     * @param ShareSettingsHelper     $shareSettings
     * @param PasswordService         $passwordModelService
     * @param PasswordRevisionService $passwordRevisionService
     */
    public function __construct(
        ShareService $modelService,
        ShareSettingsHelper $shareSettings,
        PasswordService $passwordModelService,
        PasswordRevisionService $passwordRevisionService
    ) {
        $this->modelService            = $modelService;
        $this->shareSettings           = $shareSettings;
        $this->passwordModelService    = $passwordModelService;
        $this->passwordRevisionService = $passwordRevisionService;
    }

    /**
     * @param string   $password
     * @param string   $receiver
     * @param string   $type
     * @param int|null $expires
     * @param bool     $editable
     * @param bool     $shareable
     *
     * @return \OCA\Passwords\Db\ModelInterface|\OCA\Passwords\Db\Share
     * @throws ApiException
     * @throws \Exception
     * @throws \OCP\AppFramework\Db\DoesNotExistException
     * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
     */
    public function createPasswordShare(
        string $password,
        string $receiver,
        string $type = 'user',
        int $expires = null,
        bool $editable = false,
        bool $shareable = false
    ) {
        $expires = $this->checkExpires($expires);
        $this->checkType($type);

        /** @var Password $model */
        $model = $this->passwordModelService->findByUuid($password);
        if($model->getShareId()) $editable = $this->checkSourceShare($receiver, $model);

        $this->checkIfAlreadyShared($receiver, $model);

        /** @var PasswordRevision $revision */
        $revision = $this->passwordRevisionService->findByUuid($model->getRevision(), true);
        $this->checkIfRevisionCanBeShared($revision);

        if($revision->getSseType() !== EncryptionService::SSE_ENCRYPTION_V1) {
            $this->downgradeSSE($revision, $model);
        }

        $share = $this->modelService->create($model->getUuid(), $receiver, $type, $editable, $expires, $shareable);
        $this->modelService->save($share);

        $this->setHasSharesFlag($model);

        return $share;
    }

    /**
     * @param int|null $expires
     *
     * @return int|null
     * @throws ApiException
     */
    protected function checkExpires(?int $expires) {
        if(empty($expires)) $expires = null;
        if($expires !== null && $expires < time()) throw new ApiException('Invalid expiration date', 400);

        return $expires;
    }

    /**
     * @param string $type
     *
     * @throws ApiException
     */
    protected function checkType(string $type): void {
        if($type !== 'user') throw new ApiException('Invalid share type', 400);
    }

    /**
     * @param string $receiver
     * @param        $model
     *
     * @return bool
     * @throws ApiException
     * @throws \OCP\AppFramework\Db\DoesNotExistException
     * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
     */
    protected function checkSourceShare(string $receiver, Password $model): bool {
        $sourceShare = $this->modelService->findByUuid($model->getShareId());
        if($sourceShare->getUserId() === $receiver) throw new ApiException('Invalid receiver uid', 400);
        if(!$sourceShare->isShareable() || !$this->shareSettings->get('resharing')) throw new ApiException('Entity not shareable', 403);
        if(!$sourceShare->isEditable()) return false;

        return true;
{'fraction_non_alphanumeric': 0.12294381542405469, 'fraction_numerical': 0.0027771843623157445, 'mean_word_length': 2.8435960591133007, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6982257', 'n_tokens_mistral': 2709, 'n_tokens_neox': 2611, 'n_words': 523}