Skip to content

Commit 6dc9ef7

Browse files
committed
Switch collaboration permission checks to using User objects
1 parent e616cde commit 6dc9ef7

9 files changed

Lines changed: 125 additions & 128 deletions

File tree

binaryninjaapi.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5780,7 +5780,7 @@ namespace BinaryNinja {
57805780
void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> sym, uint64_t reloc);
57815781

57825782
/*! OnAfterSnapshotDataApplied is called when loading a view from a database, after snapshot data has been applied to it.
5783-
5783+
57845784
\note This method **may** be overridden by custom BinaryViews.
57855785

57865786
\warning This method **must not** be called directly.
@@ -22509,9 +22509,9 @@ namespace BinaryNinja::Collaboration
2250922509
uint64_t GetId();
2251022510
std::string GetName();
2251122511
void SetName(const std::string& name);
22512-
void SetUsernames(const std::vector<std::string>& usernames);
22513-
bool ContainsUser(const std::string& username);
22514-
22512+
std::vector<Ref<CollabUser>> GetUsers();
22513+
void SetUsers(const std::vector<Ref<CollabUser>>& users);
22514+
bool ContainsUser(Ref<CollabUser> user);
2251522515
};
2251622516

2251722517
/*!
@@ -22692,10 +22692,11 @@ namespace BinaryNinja::Collaboration
2269222692
/*!
2269322693
Create a new group on the remote (and pull it)
2269422694
\param name Group name
22695+
\param users List of users in group
2269522696
\return Reference to the created group
2269622697
\throws RemoteException If there is an error in any request or if the remote is not connected
2269722698
*/
22698-
Ref<CollabGroup> CreateGroup(const std::string& name, const std::vector<std::string>& usernames);
22699+
Ref<CollabGroup> CreateGroup(const std::string& name, const std::vector<Ref<CollabUser>>& users = {});
2269922700

2270022701

2270122702
/*!
@@ -23143,9 +23144,9 @@ namespace BinaryNinja::Collaboration
2314323144
Ref<CollabPermission> CreateUserPermission(const std::string& userId, BNCollaborationPermissionLevel level, ProgressFunction progress = {});
2314423145
void PushPermission(Ref<CollabPermission> permission, const std::vector<std::pair<std::string, std::string>>& extraFields = {});
2314523146
void DeletePermission(Ref<CollabPermission> permission);
23146-
bool CanUserView(const std::string& username);
23147-
bool CanUserEdit(const std::string& username);
23148-
bool CanUserAdmin(const std::string& username);
23147+
bool CanUserView(Ref<CollabUser> user);
23148+
bool CanUserEdit(Ref<CollabUser> user);
23149+
bool CanUserAdmin(Ref<CollabUser> user);
2314923150
};
2315023151

2315123152
class AnalysisMergeConflict : public CoreRefCountObject<BNAnalysisMergeConflict, BNNewAnalysisMergeConflictReference, BNFreeAnalysisMergeConflict>

binaryninjacore.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8605,7 +8605,7 @@ extern "C"
86058605
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteGetGroupByName(BNRemote* remote, const char* name);
86068606
BINARYNINJACOREAPI bool BNRemoteSearchGroups(BNRemote* remote, const char* prefix, uint64_t** groupIds, char*** groupNames, size_t* count);
86078607
BINARYNINJACOREAPI bool BNRemotePullGroups(BNRemote* remote, BNProgressFunction progress, void* progressContext);
8608-
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteCreateGroup(BNRemote* remote, const char* name, const char** usernames, size_t usernameCount);
8608+
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteCreateGroup(BNRemote* remote, const char* name, BNCollaborationUser** users, size_t userCount);
86098609
BINARYNINJACOREAPI bool BNRemotePushGroup(BNRemote* remote, BNCollaborationGroup* group, const char** extraFieldKeys, const char** extraFieldValues, size_t extraFieldCount);
86108610
BINARYNINJACOREAPI bool BNRemoteDeleteGroup(BNRemote* remote, BNCollaborationGroup* group);
86118611
BINARYNINJACOREAPI BNCollaborationUser** BNRemoteGetUsers(BNRemote* remote, size_t* count);
@@ -8629,9 +8629,9 @@ extern "C"
86298629
BINARYNINJACOREAPI uint64_t BNCollaborationGroupGetId(BNCollaborationGroup* group);
86308630
BINARYNINJACOREAPI char* BNCollaborationGroupGetName(BNCollaborationGroup* group);
86318631
BINARYNINJACOREAPI void BNCollaborationGroupSetName(BNCollaborationGroup* group, const char* name);
8632-
BINARYNINJACOREAPI bool BNCollaborationGroupGetUsers(BNCollaborationGroup* group, char*** userIds, char*** usernames, size_t* count);
8633-
BINARYNINJACOREAPI bool BNCollaborationGroupSetUsernames(BNCollaborationGroup* group, const char** names, size_t count);
8634-
BINARYNINJACOREAPI bool BNCollaborationGroupContainsUser(BNCollaborationGroup* group, const char* username);
8632+
BINARYNINJACOREAPI BNCollaborationUser** BNCollaborationGroupGetUsers(BNCollaborationGroup* group, size_t* count);
8633+
BINARYNINJACOREAPI bool BNCollaborationGroupSetUsers(BNCollaborationGroup* group, BNCollaborationUser** users, size_t count);
8634+
BINARYNINJACOREAPI bool BNCollaborationGroupContainsUser(BNCollaborationGroup* group, BNCollaborationUser* user);
86358635

86368636
// CollabUser
86378637
BINARYNINJACOREAPI BNCollaborationUser* BNNewCollaborationUserReference(BNCollaborationUser* user);
@@ -8694,9 +8694,9 @@ extern "C"
86948694
BINARYNINJACOREAPI BNCollaborationPermission* BNRemoteProjectCreateUserPermission(BNRemoteProject* project, const char* userId, BNCollaborationPermissionLevel level, BNProgressFunction progress, void* progressContext);
86958695
BINARYNINJACOREAPI bool BNRemoteProjectPushPermission(BNRemoteProject* project, BNCollaborationPermission* permission, const char** extraFieldKeys, const char** extraFieldValues, size_t extraFieldCount);
86968696
BINARYNINJACOREAPI bool BNRemoteProjectDeletePermission(BNRemoteProject* project, BNCollaborationPermission* permission);
8697-
BINARYNINJACOREAPI bool BNRemoteProjectCanUserView(BNRemoteProject* project, const char* username);
8698-
BINARYNINJACOREAPI bool BNRemoteProjectCanUserEdit(BNRemoteProject* project, const char* username);
8699-
BINARYNINJACOREAPI bool BNRemoteProjectCanUserAdmin(BNRemoteProject* project, const char* username);
8697+
BINARYNINJACOREAPI bool BNRemoteProjectCanUserView(BNRemoteProject* project, BNCollaborationUser* user);
8698+
BINARYNINJACOREAPI bool BNRemoteProjectCanUserEdit(BNRemoteProject* project, BNCollaborationUser* user);
8699+
BINARYNINJACOREAPI bool BNRemoteProjectCanUserAdmin(BNRemoteProject* project, BNCollaborationUser* user);
87008700

87018701
// RemoteFile
87028702
BINARYNINJACOREAPI BNRemoteFile* BNNewRemoteFileReference(BNRemoteFile* file);

collaboration.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -878,16 +878,16 @@ void Remote::PullGroups(ProgressFunction progress)
878878
}
879879

880880

881-
Ref<CollabGroup> Remote::CreateGroup(const std::string& name, const std::vector<std::string>& usernames)
881+
Ref<CollabGroup> Remote::CreateGroup(const std::string& name, const std::vector<Ref<CollabUser>>& users)
882882
{
883-
const char** cstrNames = new const char*[usernames.size()];
884-
for (size_t i = 0; i < usernames.size(); i++)
883+
BNCollaborationUser** cUsers = new BNCollaborationUser*[users.size()];
884+
for (size_t i = 0; i < users.size(); i++)
885885
{
886-
cstrNames[i] = usernames[i].c_str();
886+
cUsers[i] = users[i]->m_object;
887887
}
888888

889-
BNCollaborationGroup* group = BNRemoteCreateGroup(m_object, name.c_str(), cstrNames, usernames.size());
890-
delete[] cstrNames;
889+
BNCollaborationGroup* group = BNRemoteCreateGroup(m_object, name.c_str(), cUsers, users.size());
890+
delete[] cUsers;
891891
if (!group)
892892
return nullptr;
893893
return new CollabGroup(group);
@@ -1076,22 +1076,38 @@ void CollabGroup::SetName(const std::string& name)
10761076
}
10771077

10781078

1079-
void CollabGroup::SetUsernames(const std::vector<std::string>& usernames)
1079+
std::vector<Ref<CollabUser>> CollabGroup::GetUsers()
10801080
{
1081-
const char** cNames = new const char*[usernames.size()];
1082-
for (size_t i = 0; i < usernames.size(); i++)
1081+
size_t count = 0;
1082+
BNCollaborationUser** users = BNCollaborationGroupGetUsers(m_object, &count);
1083+
std::vector<Ref<CollabUser>> out;
1084+
out.reserve(count);
1085+
for (size_t i = 0; i < count; i++)
1086+
{
1087+
out.push_back(new CollabUser(BNNewCollaborationUserReference(users[i])));
1088+
}
1089+
BNFreeCollaborationUserList(users, count);
1090+
return out;
1091+
}
1092+
1093+
1094+
void CollabGroup::SetUsers(const std::vector<Ref<CollabUser>>& users)
1095+
{
1096+
size_t count = users.size();
1097+
BNCollaborationUser** cUsers = new BNCollaborationUser*[count];
1098+
for (size_t i = 0; i < count; i++)
10831099
{
1084-
cNames[i] = usernames[i].c_str();
1100+
cUsers[i] = users[i]->m_object;
10851101
}
10861102

1087-
BNCollaborationGroupSetUsernames(m_object, cNames, usernames.size());
1088-
delete[] cNames;
1103+
BNCollaborationGroupSetUsers(m_object, cUsers, count);
1104+
delete[] cUsers;
10891105
}
10901106

10911107

1092-
bool CollabGroup::ContainsUser(const std::string& username)
1108+
bool CollabGroup::ContainsUser(Ref<CollabUser> user)
10931109
{
1094-
return BNCollaborationGroupContainsUser(m_object, username.c_str());
1110+
return BNCollaborationGroupContainsUser(m_object, user->m_object);
10951111
}
10961112

10971113

@@ -1566,21 +1582,21 @@ void RemoteProject::DeletePermission(Ref<CollabPermission> permission)
15661582
}
15671583

15681584

1569-
bool RemoteProject::CanUserView(const std::string& username)
1585+
bool RemoteProject::CanUserView(Ref<CollabUser> user)
15701586
{
1571-
return BNRemoteProjectCanUserView(m_object, username.c_str());
1587+
return BNRemoteProjectCanUserView(m_object, user->m_object);
15721588
}
15731589

15741590

1575-
bool RemoteProject::CanUserEdit(const std::string& username)
1591+
bool RemoteProject::CanUserEdit(Ref<CollabUser> user)
15761592
{
1577-
return BNRemoteProjectCanUserEdit(m_object, username.c_str());
1593+
return BNRemoteProjectCanUserEdit(m_object, user->m_object);
15781594
}
15791595

15801596

1581-
bool RemoteProject::CanUserAdmin(const std::string& username)
1597+
bool RemoteProject::CanUserAdmin(Ref<CollabUser> user)
15821598
{
1583-
return BNRemoteProjectCanUserAdmin(m_object, username.c_str());
1599+
return BNRemoteProjectCanUserAdmin(m_object, user->m_object);
15841600
}
15851601

15861602

python/collaboration/group.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import List, Tuple
33

44
from .. import _binaryninjacore as core
5-
from . import remote, util
5+
from . import remote, user, util
66

77

88
class Group:
@@ -70,43 +70,40 @@ def name(self, name: str):
7070
core.BNCollaborationGroupSetName(self._handle, name)
7171

7272
@property
73-
def users(self) -> List[Tuple[str, str]]:
73+
def users(self) -> List[user.User]:
7474
"""
7575
Get list of users in the group
7676
77-
:return: List of (userid, username) pairs
77+
:return: List of users
7878
"""
7979
count = ctypes.c_size_t()
80-
user_ids = ctypes.POINTER(ctypes.c_char_p)()
81-
usernames = ctypes.POINTER(ctypes.c_char_p)()
82-
if not core.BNCollaborationGroupGetUsers(self._handle, user_ids, usernames, count):
80+
result = core.BNCollaborationGroupGetUsers(self._handle, count)
81+
if not result:
8382
raise RuntimeError(util._last_error())
84-
result = []
83+
out = []
8584
for i in range(count.value):
86-
result.append((core.pyNativeStr(user_ids[i]), core.pyNativeStr(usernames[i])))
87-
core.BNFreeStringList(user_ids, count.value)
88-
core.BNFreeStringList(usernames, count.value)
89-
return result
85+
out.append(user.User(result[i]))
86+
return out
9087

9188
@users.setter
92-
def users(self, usernames: List[str]):
89+
def users(self, users: List[user.User]):
9390
"""
94-
Set the list of users in a group by their usernames.
91+
Set the list of users in a group.
9592
You will need to push the group to update the Remote.
9693
97-
:param usernames: Usernames of new group members
94+
:param users: New group members
9895
"""
99-
array = (ctypes.c_char_p * len(usernames))()
100-
for i in range(len(usernames)):
101-
array[i] = core.cstr(usernames[i])
102-
if not core.BNCollaborationGroupSetUsernames(self._handle, array, len(usernames)):
96+
array = ctypes.POINTER(core.BNCollaborationUserHandle)()
97+
for i in range(len(users)):
98+
array[i] = users[i]._handle
99+
if not core.BNCollaborationGroupSetUsers(self._handle, array, len(users)):
103100
raise RuntimeError(util._last_error())
104101

105-
def contains_user(self, username: str) -> bool:
102+
def contains_user(self, user: user.User) -> bool:
106103
"""
107-
Test if a group has a user with the given username
104+
Test if a group contains a user
108105
109-
:param username: Username of user to check membership
110-
:return: If the user is in the group
106+
:param user: User to check membership of
107+
:return: If the group contains the user
111108
"""
112-
return core.BNCollaborationGroupContainsUser(self._handle, username)
109+
return core.BNCollaborationGroupContainsUser(self._handle, user._handle)

python/collaboration/project.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..database import Database
1212
from ..filemetadata import FileMetadata
1313
from ..project import Project
14-
from . import databasesync, file, folder, permission, remote, util
14+
from . import databasesync, file, folder, permission, remote, user, util
1515

1616

1717
def _nop(*args, **kwargs):
@@ -713,35 +713,35 @@ def delete_permission(self, permission: 'permission.Permission'):
713713
if not core.BNRemoteProjectDeletePermission(self._handle, permission._handle):
714714
raise RuntimeError(util._last_error())
715715

716-
def can_user_view(self, username: str) -> bool:
716+
def can_user_view(self, user: user.User) -> bool:
717717
"""
718718
Determine if a user is in any of the view/edit/admin groups
719719
720-
:param username: Username of user to check
721-
:return: True if they are in any of those groups
720+
:param user: User to check
721+
:return: True if the user has view permission (either directly or from a group)
722722
:raises: RuntimeError if there was an error
723723
"""
724-
return core.BNRemoteProjectCanUserView(self._handle, username)
724+
return core.BNRemoteProjectCanUserView(self._handle, user._handle)
725725

726-
def can_user_edit(self, username: str) -> bool:
726+
def can_user_edit(self, user: user.User) -> bool:
727727
"""
728728
Determine if a user is in any of the edit/admin groups
729729
730-
:param username: Username of user to check
731-
:return: True if they are in any of those groups
730+
:param user: User to check
731+
:return: True if the user has edit permission (either directly or from a group)
732732
:raises: RuntimeError if there was an error
733733
"""
734-
return core.BNRemoteProjectCanUserEdit(self._handle, username)
734+
return core.BNRemoteProjectCanUserEdit(self._handle, user._handle)
735735

736-
def can_user_admin(self, username: str) -> bool:
736+
def can_user_admin(self, user: user.User) -> bool:
737737
"""
738738
Determine if a user is in the admin group
739739
740-
:param username: Username of user to check
741-
:return: True if they are in any of those groups
740+
:param user: User to check
741+
:return: True if the user has admin permission (either directly or from a group)
742742
:raises: RuntimeError if there was an error
743743
"""
744-
return core.BNRemoteProjectCanUserAdmin(self._handle, username)
744+
return core.BNRemoteProjectCanUserAdmin(self._handle, user._handle)
745745

746746
def upload_new_file(
747747
self,

python/collaboration/remote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,22 +520,22 @@ def pull_groups(self, progress: 'util.ProgressFuncType' = util.nop):
520520
if not core.BNRemotePullGroups(self._handle, util.wrap_progress(progress), None):
521521
raise RuntimeError(util._last_error())
522522

523-
def create_group(self, name: str, usernames: List[str]) -> 'group.Group':
523+
def create_group(self, name: str, users: List[user.User]) -> 'group.Group':
524524
"""
525525
Create a new group on the remote (and pull it)
526526
527527
.. note:: This function is only available to accounts with admin status on the Remote
528528
529529
:param name: Group name
530-
:param usernames: List of usernames of users in the group
530+
:param users: List of users in the group
531531
:return: Reference to the created group
532532
:raises: RuntimeError if there was an error
533533
"""
534-
c_usernames = (ctypes.c_char_p * len(usernames))()
535-
for (i, username) in enumerate(usernames):
536-
c_usernames[i] = core.cstr(username)
534+
c_users = (core.BNCollaborationUserHandle * len(users))()
535+
for (i, member) in enumerate(users):
536+
c_users[i] = member._handle
537537

538-
value = core.BNRemoteCreateGroup(self._handle, name, c_usernames, len(usernames))
538+
value = core.BNRemoteCreateGroup(self._handle, name, c_users, len(users))
539539
if value is None:
540540
raise RuntimeError(util._last_error())
541541
return group.Group(value)

0 commit comments

Comments
 (0)