Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissions are not saved in project config #18

Open
d-simon opened this issue Apr 30, 2020 · 9 comments
Open

Permissions are not saved in project config #18

d-simon opened this issue Apr 30, 2020 · 9 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@d-simon
Copy link

d-simon commented Apr 30, 2020

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
Copy link
Owner

moacode commented May 2, 2020

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 moacode self-assigned this May 2, 2020
@moacode moacode added the enhancement New feature or request label May 2, 2020
@moacode moacode added this to the 1.5.0 milestone May 2, 2020
@moacode
Copy link
Owner

moacode commented May 3, 2020

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
Copy link
Author

d-simon commented May 4, 2020

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

@d-simon
Copy link
Author

d-simon commented Jul 7, 2020

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
Copy link
Contributor

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

@moacode
Copy link
Owner

moacode commented Oct 19, 2020

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
Copy link

MoritzLost commented Aug 19, 2021

@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
Copy link
Contributor

JoshCoady commented Aug 21, 2021

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants