|
31 | 31 | #include <gromox/dbop.h> |
32 | 32 | #include <gromox/defs.h> |
33 | 33 | #include <gromox/fileio.h> |
| 34 | +#include <gromox/flat_set.hpp> |
34 | 35 | #include <gromox/mapidefs.h> |
35 | 36 | #include <gromox/mysql_adaptor.hpp> |
36 | 37 | #include <gromox/svc_common.h> |
@@ -904,6 +905,42 @@ bool mysql_plugin::check_mlist_include(const char *mlist_name, |
904 | 905 | return false; |
905 | 906 | } |
906 | 907 |
|
| 908 | +errno_t mysql_plugin::get_user_groups_rec(const char *username, std::vector<std::string> &groups) try |
| 909 | +{ |
| 910 | + gromox::maybe_flat_set<uint32_t> seen_ml; |
| 911 | + auto conn = g_sqlconn_pool.get_wait(); |
| 912 | + if (!conn) |
| 913 | + return EIO; |
| 914 | + bool round = 0; |
| 915 | + size_t rescan_begin = 0, rescan_end = 0; |
| 916 | + for (size_t i = rescan_begin; i < rescan_end || round == 0; ++i) { |
| 917 | + auto entity = round != 0 ? groups[i].c_str() : username; |
| 918 | + auto qstr = "SELECT DISTINCT m.listname FROM associations AS a " |
| 919 | + "INNER JOIN mlists AS m ON a.list_id=m.id " |
| 920 | + "WHERE a.username='" + conn->quote(entity) + "'"; |
| 921 | + if (!conn->query(qstr)) |
| 922 | + return EIO; |
| 923 | + auto result = conn->store_result(); |
| 924 | + DB_ROW row; |
| 925 | + ++rescan_begin; |
| 926 | + round = 1; |
| 927 | + while ((row = result.fetch_row()) != nullptr) { |
| 928 | + if (row[1] == nullptr) |
| 929 | + continue; |
| 930 | + auto list_id = strtoul(row[0], nullptr, 0); |
| 931 | + if (seen_ml.contains(list_id)) |
| 932 | + continue; |
| 933 | + seen_ml.emplace(list_id); |
| 934 | + groups.emplace_back(row[1]); |
| 935 | + ++rescan_end; |
| 936 | + } |
| 937 | + } |
| 938 | + return 0; |
| 939 | +} catch (const std::bad_alloc &e) { |
| 940 | + mlog(LV_ERR, "E-1731: ENOMEM"); |
| 941 | + return ENOMEM; |
| 942 | +} |
| 943 | + |
907 | 944 | errno_t mysql_adaptor_meta(const char *u, unsigned int p, sql_meta_result &r) |
908 | 945 | { |
909 | 946 | return le_mysql_plugin->meta(u, p, r); |
@@ -1055,3 +1092,8 @@ errno_t mysql_adaptor_mda_domain_list(sql_domain_set &v) |
1055 | 1092 | { |
1056 | 1093 | return le_mysql_plugin->mda_domain_list(v); |
1057 | 1094 | } |
| 1095 | + |
| 1096 | +errno_t mysql_adaptor_get_user_groups_rec(const char *user, std::vector<std::string> &groups) |
| 1097 | +{ |
| 1098 | + return le_mysql_plugin->get_user_groups_rec(user, groups); |
| 1099 | +} |
0 commit comments