# Group Assignment SQL Query

**URL:** https://meta.discourse.org/t/group-assignment-sql-query/25610
**Category:** Feature
**Created:** [February 24, 2015, 2:35pm UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610 "2015-02-24T14:35:58Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![thomaspurchas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thomaspurchas/32/115664_2.png) [@thomaspurchas](https://meta.discourse.org/u/thomaspurchas)
#### Post date: [February 24, 2015, 2:35pm UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/1 "2015-02-24T14:35:58Z")

</div>

Before I get to my feature request, I would like to take the time to thank the Discourse team for their work. Discourse is an epic piece of software, and so far everyone I have introduced it to has nothing but high praise for it.

# Feature

Would it be possible to extend the Badge Query options to groups.

Currently groups can either be assigned by email domain or manually. Adding SQL queries would extend that to support anything an admin can imagine.

## Use Case

I currently use SSO to integrate discourse into the rest of our society’s site. Our society’s site manages a number of things such as rotas, POS and film schedules.

To do this our site used groups to assign rights to various members. I would like to extend these groups over to discourse.

Currently I am using the `custom_fields` provided by SSO and SQL queries to assign badges to people based on their roles. Adding SQL queries to groups would also allow me to assign groups based on their roles.

This then allows me to create private categories so that teams can discuss topics that are only pertinent to that team.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 24, 2015, 10:42pm UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/2 "2015-02-24T22:42:55Z")

</div>

That is a massive change that is not on the roadmap, instead I would strongly recommend you look into writing a plugin to achieve your goal, we are totally open to adding more hooks as needed.

---

<div class="post-metadata">

### Author: ![thomaspurchas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thomaspurchas/32/115664_2.png) [@thomaspurchas](https://meta.discourse.org/u/thomaspurchas)
#### Post date: [February 25, 2015, 12:00am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/3 "2015-02-25T00:00:06Z")

</div>

Is their a comprehensive list of hooks anywhere? I’ve been looking around and there are a couple of topics that list them, but I can’t seem to find a canonical list anywhere.

---

<div class="post-metadata">

### Author: ![sigurdur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sigurdur/32/122516_2.png) [@sigurdur](https://meta.discourse.org/u/sigurdur)
#### Post date: [February 25, 2015, 12:09am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/4 "2015-02-25T00:09:01Z")

</div>

> [@thomaspurchas](#):
>
> Is their a comprehensive list of hooks anywhere?

Full list of hooks right here:  
[https://meta.discourse.org/t/making-discourse-more-pluggable-by-adding-more-callback-triggers/25579](https://meta.discourse.org/t/making-discourse-more-pluggable-by-adding-more-callback-triggers/25579)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [February 25, 2015, 12:12am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/5 "2015-02-25T00:12:20Z")

</div>

I think (adding) a `user_verified` hook would work here, as @thomaspurchas likely only wants to deal with users that have legit emails.

---

<div class="post-metadata">

### Author: ![thomaspurchas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thomaspurchas/32/115664_2.png) [@thomaspurchas](https://meta.discourse.org/u/thomaspurchas)
#### Post date: [February 25, 2015, 12:17am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/7 "2015-02-25T00:17:16Z")

</div>

Also a `user_edited` hook :smile:

As my users are created my SSO would the `user_verified` hook get called as there is no explicit email validation stage?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [February 25, 2015, 12:48am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/8 "2015-02-25T00:48:44Z")

</div>

Yeah, I was thinking a on\_save trigger that checks if `activated` transitioned from false to true.

---

<div class="post-metadata">

### Author: ![sigurdur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sigurdur/32/122516_2.png) [@sigurdur](https://meta.discourse.org/u/sigurdur)
#### Post date: [February 25, 2015, 5:31pm UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/9 "2015-02-25T17:31:48Z")

</div>

Well, there we go.. I created a PR with 3 user hooks, `user_created`, `user_updated` and `user_deleted`.  
[https://github.com/discourse/discourse/pull/3237](https://github.com/discourse/discourse/pull/3237)

I’ll be using those triggers in my plugin, but it looks like @thomaspurchas will be able to use it as well.. :smile:

---

<div class="post-metadata">

### Author: ![thomaspurchas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thomaspurchas/32/115664_2.png) [@thomaspurchas](https://meta.discourse.org/u/thomaspurchas)
#### Post date: [February 25, 2015, 5:56pm UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/10 "2015-02-25T17:56:47Z")

</div>

Thanks for that @sigurdur!

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [July 17, 2018, 6:09am UTC](https://meta.discourse.org/t/group-assignment-sql-query/25610/11 "2018-07-17T06:09:43Z")

</div>

Continued here:

> [@What if badges could optionally synchronize group membership?](https://meta.discourse.org/t/what-if-badges-could-optionally-synchronize-group-membership/92232):
>
> I was looking at this topic today: Thinking through the problem one area Discourse is a bit weak is around defining “dynamic” groups of users. Give me a group of users that signed up more than 1 month ago Give me a group of users that posted at least 10 times And so on. Badges on the other hand have very rich support for this “dynamic” nature once the fancy SQL is enabled. Having an “extra” tick box on badges to bridge a badge to a group would add a lot of power. We have no way of a…
