moacode / craft-fab-permissions

Restrict access to fields and tabs for particular user groups across your Craft elements.
Other
16 stars 9 forks source link

Permissions are not saved in project config #18

Open d-simon opened 4 years ago

d-simon commented 4 years ago

Hi there, great plugin! :-)

Is it by design, that permissions are not saved into the project config? Not sure if this is a bug. This is an essential feature for a client I am using this for (admin changes are disallowed on production). Thanks for letting me know. I can provide more details (versions etc) and a project.yml if this is indeed a bug.

(cc: @znezniV)

moacode commented 4 years ago

Hi @d-simon, unfortunately this isn't a bug. When I first made the plugin project config was quite new and I didn't look at implementing it.

In saying that, I've had a few requests for this now so I will dedicate some time to adding support over the next couple of weeks!

moacode commented 4 years ago

Hi @d-simon and @znezniV, I've added support for project config which is available as release 1.5.0-beta.1.

composer require thejoshsmith/craft-fab-permissions:1.5.0-beta.1

This was quite a large change and involved modifying the database structure and how the data is stored. You may notice a slight performance decrease (I had to remove DB batch inserts and cascades), but it shouldn't be too bad.

I've added a migration that will auto migrate your DB and rebuild your project config yaml file so you should be able to install the new version and carry on as normal.

Note: As this is a beta release, I recommend backing up your DB and giving the new version a good test and DO NOT use in production! I'll be doing some more testing over the next few days and will release a stable version if it all looks good. If you come across any issues, please let me know!

d-simon commented 4 years ago

Amazing! Thank you for getting this in! I will have time to test it this towards the end of the week. Cheers! 🙌

d-simon commented 3 years ago

Hi @thejoshsmith I wanted to give you a brief update: The project we are using this in is currently on hold and as such I don't currently have the time to test this. Apologies. We are estimating that we'll continue with the next development phase in September. Will give you an update when I get to test this.

JoshCoady commented 3 years ago

@thejoshsmith any news on this? I could give it a test, but I need it to be compatible with Craft v3.5

moacode commented 3 years ago

Hey @JoshCoady, it's been a while since I've looked at this but now that the stable release is compatible with Craft 3.5 I can bring this one up to date. I'll aim to do that this week as it would be great to get someone to test it.

MoritzLost commented 2 years ago

@thejoshsmith Are you still planning on implementing support for project-config? Looks like PR #26 is mostly working, any chance to get it merged?

Without project config support this module doesn't work at all for sites that use a deployment pipeline, which is unfortunate since otherwise the module is doing a great job!

JoshCoady commented 2 years ago

@MoritzLost it's a bit of a pain, but what we've done while awaiting project config support is implement any changes in a migration. Rather than worry about updating, we've taken the tact of just wiping out all the permissions data and then recreating it all, including whatever changes are needed. But we dont use it extensively

<?php

namespace craft\contentmigrations;

use Craft;
use craft\db\Migration;
use thejoshsmith\fabpermissions\records\FabPermissionsRecord;
use thejoshsmith\fabpermissions\services\Fab;

/**
 * m210519_041259_permissionsUriAndMetaFieldsRedo migration.
 */
class m210519_041259_permissions_uri_and_meta_fields_redo extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $fabPermissionsRecord = new FabPermissionsRecord();

        $articleEntryTypes = Craft::$app->sections->getEntryTypesByHandle("article");
        $articleEntryType = reset($articleEntryTypes);
        $layoutId = $articleEntryType->fieldLayoutId;
        $currentSite = Craft::$app->sites->getCurrentSite();

        $adminId = null;
        $editorsId = Craft::$app->getUserGroups()->getGroupByHandle('editors')->id;
        $managersId = Craft::$app->getUserGroups()->getGroupByHandle('managers')->id;
        $writersId = Craft::$app->getUserGroups()->getGroupByHandle('writers')->id;
        $imagersId = Craft::$app->getUserGroups()->getGroupByHandle('imagers')->id;
        $copywritersId = Craft::$app->getUserGroups()->getGroupByHandle('copywriters')->id;

        $metaTitleId = Craft::$app->getFields()->getFieldByHandle('metaTitle')->id;
        $teaserTitleId = Craft::$app->getFields()->getFieldByHandle('teaserTitle')->id;
        $metaDescriptionId = Craft::$app->getFields()->getFieldByHandle('metaDescription')->id;
        $summaryId = Craft::$app->getFields()->getFieldByHandle('summary')->id;

        $canonicalUriId = Craft::$app->getFields()->getFieldByHandle('canonicalUri')->id;

        $fields = array_values(
            array_intersect($fabPermissionsRecord->attributes(), [
                    'layoutId',
                    'tabId',
                    'fieldId',
                    'siteId',
                    'userGroupId',
                    Fab::$viewPermissionHandle,
                    Fab::$editPermissionHandle,
                ]
            ));

        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $adminId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $editorsId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $managersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $writersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $imagersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $canonicalUriId,
            $currentSite->id,
            $copywritersId,
            '1',
            '0',
        ];

        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $adminId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $editorsId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $managersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $writersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $imagersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaTitleId,
            $currentSite->id,
            $copywritersId,
            '1',
            '1',
        ];

        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $adminId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $editorsId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $managersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $writersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $imagersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $teaserTitleId,
            $currentSite->id,
            $copywritersId,
            '1',
            '1',
        ];

        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $adminId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $editorsId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $managersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $writersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $imagersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $metaDescriptionId,
            $currentSite->id,
            $copywritersId,
            '1',
            '1',
        ];

        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $adminId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $editorsId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $managersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $writersId,
            '1',
            '0',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $imagersId,
            '1',
            '1',
        ];
        $fabPermissionsData[] = [
            $layoutId,
            null,
            $summaryId,
            $currentSite->id,
            $copywritersId,
            '1',
            '1',
        ];

        // first clear out all data in the table
        Craft::$app->db->createCommand()->truncateTable(FabPermissionsRecord::tableName())->execute();

        // now insert all of the permissions as they should currently be
        Craft::$app->db->createCommand()->batchInsert(FabPermissionsRecord::tableName(), $fields, $fabPermissionsData)->execute();
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        Craft::$app->db->createCommand()->truncateTable(FabPermissionsRecord::tableName())->execute();
        return true;
    }
}
MoritzLost commented 2 years ago

Thanks a bunch @JoshCoady! That's a smart workaround, I'm probably gonna use that for the time being.