# Convert a category to private and add users to a specific group with access

**URL:** https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463
**Category:** Support
**Created:** [January 4, 2024, 3:11am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463 "2024-01-04T03:11:52Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 4, 2024, 3:11am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/1 "2024-01-04T03:11:53Z")

</div>

The title might be a bit cryptic, but the context is to convert a category to “private” but have a way to identify all users that participated in it in the past \<timeframe\> and add them to a group so that it can be set as having access.

Let’s say the group can be created prior to this.

What is currently supported:

- Create a group
- Assign permissions for a group to a category
- Alter the category permissions

What is missing is a way to say “assign these users to a group”, based on their interaction in this category in a specific timeframe.

Maybe there is a query or code that I can execute that can identify those users and assign them to a group?

---

<div class="post-metadata">

### Author: ![jericson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jericson/32/116215_2.png) [@jericson](https://meta.discourse.org/u/jericson)
#### Post date: [January 4, 2024, 6:20am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/2 "2024-01-04T06:20:35Z")

</div>

> [@Crius](#):
>
> identify all users that participated in it in the past

I’ve used a [Data Explorer](https://meta.discourse.org/t/32566?silent=true) query to figure out who participated in a topic based on `user_actions`:

```plaintext
-- [params]
-- int :topic = 3620561

select user_id, 
       sum(action_type) actions,
       sum(case action_type when 5 then 1 else 0 end) replies 
from user_actions ua
     join users u on u.id = ua.user_id
where target_topic_id = :topic
      and moderator is false
      and admin is false
group by ua.user_id

```

That includes actions such as replies, likes, edits and mentions. I _also_ have a query that includes poll responses. This was for gathering leads, but I expect some of the people who did something besides reply might have a claim on the topic. Defining “participated” might be a useful exercise.

To expand to the entire category, join with `topics t on target_topic_id = t.id` and add `category_id` to the where clause. Shouldn’t be hard to define the timeframe based on `ua.created_at`.

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 8, 2024, 4:17pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/3 "2024-01-08T16:17:51Z")

</div>

Thank you @jericson, this is exactly what I was looking for.

I am guessing that I could feed the results to a query that adds all users to a group then.  
Is there anyone that have had experience doing that? Maybe it’s better to brutally copy-paste the user ids and run a ruby function if there is something available?

---

<div class="post-metadata">

### Author: ![jericson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jericson/32/116215_2.png) [@jericson](https://meta.discourse.org/u/jericson)
#### Post date: [January 8, 2024, 4:52pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/4 "2024-01-08T16:52:03Z")

</div>

> [@Crius](#):
>
> Maybe it’s better to brutally copy-paste the user ids and run a ruby function

That’s usually my preferred solution. But it probably makes more sense to pull a page from [the bulk operations examples](https://meta.discourse.org/t/administrative-bulk-operations/118349) and apply the `where` clause on the `User` object in rails. (Having a test environment for a time like this helps!)

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [January 8, 2024, 4:55pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/5 "2024-01-08T16:55:44Z")

</div>

Depending how many there are, you can paste a comma separated list into the Add Users to the group box, if that’s any use?

(There’s also an Automation to add users to a group based on a badge, which you could use this SQL as a base for. That might be a little convoluted if you’re happy with the Rails option though)

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 8, 2024, 4:57pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/6 "2024-01-08T16:57:07Z")

</div>

> [@jericson](#):
>
> Having a test environment for a time like this helps!

Just brought the staging environment app for this reason (and also testing what breaks with ember 5 😛 )

I’ll dig into it in the next few days, “real job” allowing me the time 😃

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 16, 2024, 11:15am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/8 "2024-01-16T11:15:56Z")

</div>

Just to leave the query completed in case others need it:

```plaintext
select ua.user_id, u.username, sum(action_type) actions, sum(case action_type when 5 then 1 else 0 end) replies from user_actions ua 
join users u on u.id = ua.user_id
join topics t on target_topic_id = t.id
where ua.created_at > 'YYYY-MM-DD'::date and t.category_id = CATEGORY_ID
group by ua.user_id, u.username;

```

Just replace the `YYY-MM-DD` with your cutoff date and `CATEGORY_ID` with your category ID.

The result will show the user\_id, username, actions, and replies.

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 16, 2024, 11:18am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/9 "2024-01-16T11:18:28Z")

</div>

Can you point me to that script @JammyDodger ?

I can’t see it in the link pasted by @jericson

I can extract a list of users (it will be around 250).

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [January 17, 2024, 9:12am UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/10 "2024-01-17T09:12:22Z")

</div>

The options I suggested are available in the UI, though the second one needs the [Discourse Automation](https://meta.discourse.org/t/discourse-automation/195773) plugin (it’s a good plugin though 🙂)

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 18, 2024, 12:41pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/11 "2024-01-18T12:41:38Z")

</div>

I am sorry but I don’t understand how that could help me.

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/7/2/e72fe530bf947cf9a768717eb45afaa2823bd14d.png)

I have a list of user IDs (or usernames even) and I need to add them to a group.  
I don’t see in the current automations a use case that could help me. There is only adding to group based on badges or custom field but that only means adding a further step (adding a badge to all those users or adding a custom field to users and then adding a value to the specific users I want).

Is there not a bulk\_add\_user\_to\_group function or even a add\_user\_to\_group that I can just call in a loop maybe?

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [January 18, 2024, 1:03pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/12 "2024-01-18T13:03:29Z")

</div>

> [@Crius](#):
>
> but that only means adding a further step

Yeah, it would be a little more convoluted doing it that way. But if you have a shortish list you can export the explorer results as a CSV, open up in notepad, and copy&paste them into the Add Users box:

 ![results in notepad](https://global.discourse-cdn.com/meta/original/4X/0/f/2/0f2bbd1c2d6ffba7773ccb401e3cd80feaab17e8.png)

 ![paste into Add Users](https://global.discourse-cdn.com/meta/original/4X/8/c/3/8c33a151b3fde2e744a2eba823baf487285223ba.png)

You might be able to do 250 in one go, though I haven’t tried with that many. A handful of batches should cover it though, if not.

* * *

It should also be possible through the API using a PUT request to the eg `/groups/49/members.json` endpoint (you’ll need your own group\_id) with a list of your usernames/emails.

---

<div class="post-metadata">

### Author: ![Crius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crius/32/317214_2.png) [@Crius](https://meta.discourse.org/u/Crius)
#### Post date: [January 18, 2024, 1:56pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/13 "2024-01-18T13:56:16Z")

</div>

Worked perfectly, I forgot you could add multiple usernames to the UI 🙂

For others, to export the psql query result simply run:

```plaintext
psql
\o /home/discourse/output.txt
<your query here>
\o
exit

```

Now in /home/discourse/output.txt you will have the result of the query as it would be printed on screen.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [February 17, 2024, 1:57pm UTC](https://meta.discourse.org/t/convert-a-category-to-private-and-add-users-to-a-specific-group-with-access/290463/14 "2024-02-17T13:57:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
