Skip to content

Commit bf481c8

Browse files
committed
exmdb: rework get_mbox_perm queries (2/2)
Rewrite the mlist membership permission set construction. The sections now complete in: * SELECTs: 1255 µs * mlist: 399 µs (e.g. with user in 1 group in 1 group) References: DESK-3893, DESK-3984
1 parent b399f91 commit bf481c8

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

exch/exmdb/store.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ BOOL exmdb_server::remove_store_properties(const char *dir,
219219
BOOL exmdb_server::get_mbox_perm(const char *dir,
220220
const char *username, uint32_t *ppermission) try
221221
{
222-
char sql_string[128];
223-
224222
if (!exmdb_server::is_private())
225223
return FALSE;
226224
auto pdb = db_engine_get_db(dir);
@@ -272,23 +270,24 @@ BOOL exmdb_server::get_mbox_perm(const char *dir,
272270
if (fid == PRIVATE_FID_IPMSUBTREE && perm & frightsOwner)
273271
*ppermission |= frightsGromoxStoreOwner;
274272
}
275-
pstmt.finalize();
276273

277274
/* add in mlist permissions(?) */
278-
snprintf(sql_string, std::size(sql_string), "SELECT "
279-
"username, permission FROM permissions");
280-
pstmt = pdb->prep(sql_string);
281-
if (pstmt == nullptr)
282-
return FALSE;
283-
while (pstmt.step() == SQLITE_ROW) {
284-
auto ben = pstmt.col_text(0);
285-
if (!mysql_adaptor_check_mlist_include(ben, username))
286-
continue;
287-
auto perm = pstmt.col_uint64(1);
288-
auto fid = pstmt.col_uint64(2);
289-
*ppermission |= perm;
290-
if (fid == PRIVATE_FID_IPMSUBTREE && perm & frightsOwner)
291-
*ppermission |= frightsGromoxStoreOwner;
275+
std::vector<std::string> group_memberships;
276+
auto err = mysql_adaptor_get_user_groups_rec(username, group_memberships);
277+
if (err != 0)
278+
return false;
279+
for (auto &&group : group_memberships) {
280+
pstmt.reset();
281+
pstmt.bind_text(1, group.c_str());
282+
while (pstmt.step() == SQLITE_ROW) {
283+
auto fid = pstmt.col_uint64(0);
284+
if (seen_fid.find(fid) != seen_fid.end())
285+
continue;
286+
auto perm = pstmt.col_uint64(1);
287+
*ppermission |= perm;
288+
if (fid == PRIVATE_FID_IPMSUBTREE && perm & frightsOwner)
289+
*ppermission |= frightsGromoxStoreOwner;
290+
}
292291
}
293292
pstmt.finalize();
294293
pdb.reset();

exch/mysql_adaptor/sql2.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -911,19 +911,18 @@ errno_t mysql_plugin::get_user_groups_rec(const char *username, std::vector<std:
911911
auto conn = g_sqlconn_pool.get_wait();
912912
if (!conn)
913913
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 "
914+
groups.clear();
915+
groups.emplace_back(username);
916+
size_t rescan_begin = 0, rescan_end = 1;
917+
for (size_t i = rescan_begin; i < rescan_end; ++i) {
918+
auto qstr = "SELECT DISTINCT m.id, m.listname FROM associations AS a "
919919
"INNER JOIN mlists AS m ON a.list_id=m.id "
920-
"WHERE a.username='" + conn->quote(entity) + "'";
920+
"WHERE a.username='" + conn->quote(groups[i]) + "'";
921921
if (!conn->query(qstr))
922922
return EIO;
923923
auto result = conn->store_result();
924924
DB_ROW row;
925925
++rescan_begin;
926-
round = 1;
927926
while ((row = result.fetch_row()) != nullptr) {
928927
if (row[1] == nullptr)
929928
continue;
@@ -935,6 +934,7 @@ errno_t mysql_plugin::get_user_groups_rec(const char *username, std::vector<std:
935934
++rescan_end;
936935
}
937936
}
937+
groups.erase(groups.begin());
938938
return 0;
939939
} catch (const std::bad_alloc &e) {
940940
mlog(LV_ERR, "E-1731: ENOMEM");

0 commit comments

Comments
 (0)