1

I need to avoid certain group to publish a structure group but I don't know how to get that user to check if belongs to a certaing group. This is the code I have:

    public RestrictPublishInStructureGroup()
    {
        SubScribe();
    }

    private void SubScribe()
    {
        EventSystem.SubscribeAsync<StructureGroup, PublishEventArgs>(OnPublicationTargetPublish,EventPhases.Initiated);
    }

    private void OnPublicationTargetPublish(StructureGroup structureGroup, PublishEventArgs e, EventPhases phase)
    {
        if (!e.Targets.Where(x => x.Title.ToLower() == pubTargetStaging).Any() && !e.Targets.Where(x => x.Title.ToLower() == pubTargetLive).Any())
            return;

        switch (structureGroup.Title)
        {
            case "Home":
                break;
            case "030 Personeelsinfo":
                break;
        }
    }
2

Even if you can do this using an event, you should first consider whether it is better to use Target Type permissions to manage this. There you can explicitly determine which users/groups can publish to a given target type. You won't need to write any code.

| improve this answer | |
  • I agree - Surely it would be better to let members of the group know that aren't publishing to a Target rather than have them believe they are and possibly not capture that they were not successful. My preference - if it can't be simply permission'd out per Dom's suggestion would be to extend the interface and even show the target as 'unavailable' under whatever business rules apply – Dylan .. Mark Saunders Jun 3 at 15:25
  • Hello Dominic and Dylan. You are right but I commited an error in the description. I would like to avoid certain groups to publish certain structure groups and my problem is when i try to get the user that is triggering that action. Sorry for the confusion – Ignacio Tome Jun 4 at 9:41
  • @IgnacioTome one of the best features of Stack Exchange is that you can improve your question based on insights that you get from people's responses. If you can edit your question to improve it, that helps everyone. – Dominic Cronin Jun 7 at 16:52
2

Try using Session.AccessToken.GroupMemberships.

In general, Session.AccessToken provides access to the CM Access Token, which may look like a regular User object (class AccessToken inherits from class User), but has some additional properties and, most importantly, its GroupMemberships is an expanded set of Group Memberships, i.e. it also contains indirect Group memberships and even the ones which are resolved through external claim mappings.

| improve this answer | |
  • Hello Rick. Could you please give me an example of how to use it? – Ignacio Tome Jun 4 at 8:57
  • Ignacio, please check the documentation; use Visual Studio to check the method, object, or properties; or otherwise give it a try! If you have issues with Session.AccessToken, you can ask additional questions. – Alvin Reyes Jun 7 at 6:20
1

I don't think this information is available in the event you are trying to implement. Have you considered implementing the save event for the PublishTransaction instead?

For example:

// ...
EventSystem.SubscribeAsync<PublishTransaction, PublishEventArgs>(OnPublishTransactionPublish, EventPhases.TransactionCommitted);
// ...

private void OnPublishTransactionPublish(PublishTransaction subject, PublishEventArgs e, EventPhases phase)
{
    // get the person who created the publish transaction (= the one who published the item)
    var creator = subject.Creator;
    // get the items being published
    var publishedItems = e.Items;
}

By the way: the combination of an async event in the an initiated phase is a bit unusual. Normally, initiated is used to prevent something from happening, but in an async event this isn't possible, I believe.

| improve this answer | |
  • Hello Quirijn, I tried the code you shared with me but it was never triggered when I published a structure group. So I changed the PublishTransaction object for a StructureGroup object but then I still have the probelm that I cannot get the user that is triggering the publish action. – Ignacio Tome Jun 4 at 8:46
  • Perhaps the publisher is running on a different server? This event is triggered by the publisher, not the core service. – Quirijn Jun 5 at 11:04
1

I finally get the solution with comments above.

    private void SubScribe()
    {
        EventSystem.Subscribe<StructureGroup, PublishOrUnPublishEventArgs>(OnPublishTransactionPublish, EventPhases.Initiated);
    }
    private void OnPublishTransactionPublish(StructureGroup structureGroup, PublishOrUnPublishEventArgs e, EventPhases phase)
    {
        if (structureGroup == null)
            return;

        // get the person who created the publish transaction (= the one who published the item)
        var creator = new User(structureGroup.Session.AccessToken.Id, structureGroup.Session);
        var groups = creator.GroupMemberships;
        ....
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.