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;
        }
    }

4 Answers 4

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.

3
  • 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 Jun 3, 2020 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 Jun 4, 2020 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. Jun 7, 2020 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.

2
  • Hello Rick. Could you please give me an example of how to use it? Jun 4, 2020 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. Jun 7, 2020 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.

2
  • 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. Jun 4, 2020 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, 2020 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;
        ....
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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