Skip to content

Commit 534e2c5

Browse files
committed
Change Group to use User objects
1 parent fc064b2 commit 534e2c5

7 files changed

Lines changed: 56 additions & 72 deletions

File tree

binaryninjaapi.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22462,7 +22462,7 @@ namespace BinaryNinja::Collaboration
2246222462
uint64_t GetId();
2246322463
std::string GetName();
2246422464
void SetName(const std::string& name);
22465-
void SetUsernames(const std::vector<std::string>& usernames);
22465+
void SetUsers(const std::vector<Ref<CollabUser>>& users);
2246622466
bool ContainsUser(Ref<CollabUser> user);
2246722467

2246822468
};
@@ -22645,10 +22645,11 @@ namespace BinaryNinja::Collaboration
2264522645
/*!
2264622646
Create a new group on the remote (and pull it)
2264722647
\param name Group name
22648+
\param users List of users in group
2264822649
\return Reference to the created group
2264922650
\throws RemoteException If there is an error in any request or if the remote is not connected
2265022651
*/
22651-
Ref<CollabGroup> CreateGroup(const std::string& name, const std::vector<std::string>& usernames);
22652+
Ref<CollabGroup> CreateGroup(const std::string& name, const std::vector<Ref<CollabUser>>& users = {});
2265222653

2265322654

2265422655
/*!

binaryninjacore.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8590,7 +8590,7 @@ extern "C"
85908590
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteGetGroupByName(BNRemote* remote, const char* name);
85918591
BINARYNINJACOREAPI bool BNRemoteSearchGroups(BNRemote* remote, const char* prefix, uint64_t** groupIds, char*** groupNames, size_t* count);
85928592
BINARYNINJACOREAPI bool BNRemotePullGroups(BNRemote* remote, BNProgressFunction progress, void* progressContext);
8593-
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteCreateGroup(BNRemote* remote, const char* name, const char** usernames, size_t usernameCount);
8593+
BINARYNINJACOREAPI BNCollaborationGroup* BNRemoteCreateGroup(BNRemote* remote, const char* name, BNCollaborationUser** users, size_t userCount);
85948594
BINARYNINJACOREAPI bool BNRemotePushGroup(BNRemote* remote, BNCollaborationGroup* group, const char** extraFieldKeys, const char** extraFieldValues, size_t extraFieldCount);
85958595
BINARYNINJACOREAPI bool BNRemoteDeleteGroup(BNRemote* remote, BNCollaborationGroup* group);
85968596
BINARYNINJACOREAPI BNCollaborationUser** BNRemoteGetUsers(BNRemote* remote, size_t* count);
@@ -8614,8 +8614,8 @@ extern "C"
86148614
BINARYNINJACOREAPI uint64_t BNCollaborationGroupGetId(BNCollaborationGroup* group);
86158615
BINARYNINJACOREAPI char* BNCollaborationGroupGetName(BNCollaborationGroup* group);
86168616
BINARYNINJACOREAPI void BNCollaborationGroupSetName(BNCollaborationGroup* group, const char* name);
8617-
BINARYNINJACOREAPI bool BNCollaborationGroupGetUsers(BNCollaborationGroup* group, char*** userIds, char*** usernames, size_t* count);
8618-
BINARYNINJACOREAPI bool BNCollaborationGroupSetUsernames(BNCollaborationGroup* group, const char** names, size_t count);
8617+
BINARYNINJACOREAPI BNCollaborationUser** BNCollaborationGroupGetUsers(BNCollaborationGroup* group, size_t* count);
8618+
BINARYNINJACOREAPI bool BNCollaborationGroupSetUsers(BNCollaborationGroup* group, BNCollaborationUser** users, size_t count);
86198619
BINARYNINJACOREAPI bool BNCollaborationGroupContainsUser(BNCollaborationGroup* group, BNCollaborationUser* user);
86208620

86218621
// CollabUser

collaboration.cpp

Lines changed: 13 additions & 12 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,16 +1076,17 @@ void CollabGroup::SetName(const std::string& name)
10761076
}
10771077

10781078

1079-
void CollabGroup::SetUsernames(const std::vector<std::string>& usernames)
1079+
void CollabGroup::SetUsers(const std::vector<Ref<CollabUser>>& users)
10801080
{
1081-
const char** cNames = new const char*[usernames.size()];
1082-
for (size_t i = 0; i < usernames.size(); i++)
1081+
size_t count = users.size();
1082+
BNCollaborationUser** cUsers = new BNCollaborationUser*[count];
1083+
for (size_t i = 0; i < count; i++)
10831084
{
1084-
cNames[i] = usernames[i].c_str();
1085+
cUsers[i] = users[i]->m_object;
10851086
}
10861087

1087-
BNCollaborationGroupSetUsernames(m_object, cNames, usernames.size());
1088-
delete[] cNames;
1088+
BNCollaborationGroupSetUsers(m_object, cUsers, count);
1089+
delete[] cUsers;
10891090
}
10901091

10911092

python/collaboration/group.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,33 @@ 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

105102
def contains_user(self, user: user.User) -> bool:

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)

rust/src/collaboration/group.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,32 @@ impl RemoteGroup {
5656
}
5757

5858
/// Get list of users in the group
59-
pub fn users(&self) -> Result<(Array<BnString>, Array<BnString>), ()> {
60-
let mut usernames = std::ptr::null_mut();
61-
let mut user_ids = std::ptr::null_mut();
59+
pub fn users(&self) -> Result<Array<RemoteUser>, ()> {
6260
let mut count = 0;
6361
// TODO: This should only fail if collaboration is not supported.
6462
// TODO: Because you should not have a RemoteGroup at that point we can ignore?
65-
let success = unsafe {
66-
BNCollaborationGroupGetUsers(
67-
self.handle.as_ptr(),
68-
&mut user_ids,
69-
&mut usernames,
70-
&mut count,
71-
)
72-
};
73-
success
74-
.then(|| unsafe {
75-
let ids = Array::new(user_ids, count, ());
76-
let users = Array::new(usernames, count, ());
77-
(ids, users)
78-
})
63+
let result = unsafe { BNCollaborationGroupGetUsers(self.handle.as_ptr(), &mut count) };
64+
(!result.is_null())
65+
.then(|| unsafe { Array::new(result, count, ()) })
7966
.ok_or(())
8067
}
8168

8269
// TODO: Are any permissions required to the set the remote group users?
83-
/// Set the list of users in a group by their usernames.
70+
/// Set the list of users in a group.
8471
/// You will need to push the group to update the Remote.
85-
pub fn set_users<I>(&self, usernames: I) -> Result<(), ()>
72+
pub fn set_users<I>(&self, users: I) -> Result<(), ()>
8673
where
87-
I: IntoIterator<Item = String>,
74+
I: IntoIterator<Item = Ref<RemoteUser>>,
8875
{
89-
let usernames: Vec<_> = usernames.into_iter().map(|u| u.to_cstr()).collect();
90-
let mut usernames_raw: Vec<_> = usernames.iter().map(|s| s.as_ptr()).collect();
76+
let mut users_raw: Vec<_> = users.into_iter().map(|s| s.handle.as_ptr()).collect();
9177
// TODO: This should only fail if collaboration is not supported.
9278
// TODO: Because you should not have a RemoteGroup at that point we can ignore?
9379
// TODO: Do you need any permissions to do this?
9480
let success = unsafe {
95-
BNCollaborationGroupSetUsernames(
81+
BNCollaborationGroupSetUsers(
9682
self.handle.as_ptr(),
97-
usernames_raw.as_mut_ptr(),
98-
usernames_raw.len(),
83+
users_raw.as_mut_ptr(),
84+
users_raw.len(),
9985
)
10086
};
10187
success.then_some(()).ok_or(())

rust/src/collaboration/remote.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,20 @@ impl Remote {
515515
/// # Arguments
516516
///
517517
/// * `name` - Group name
518-
/// * `usernames` - List of usernames of users in the group
519-
pub fn create_group<I>(&self, name: &str, usernames: I) -> Result<Ref<RemoteGroup>, ()>
518+
/// * `users` - List of users in the group
519+
pub fn create_group<I>(&self, name: &str, users: I) -> Result<Ref<RemoteGroup>, ()>
520520
where
521-
I: IntoIterator<Item = String>,
521+
I: IntoIterator<Item = Ref<RemoteUser>>,
522522
{
523523
let name = name.to_cstr();
524-
let usernames: Vec<_> = usernames.into_iter().map(|s| s.to_cstr()).collect();
525-
let mut username_ptrs: Vec<_> = usernames.iter().map(|s| s.as_ptr()).collect();
524+
let mut user_ptrs: Vec<_> = users.into_iter().map(|s| s.handle.as_ptr()).collect();
526525

527526
let value = unsafe {
528527
BNRemoteCreateGroup(
529528
self.handle.as_ptr(),
530529
name.as_ptr(),
531-
username_ptrs.as_mut_ptr(),
532-
username_ptrs.len(),
530+
user_ptrs.as_mut_ptr(),
531+
user_ptrs.len(),
533532
)
534533
};
535534
NonNull::new(value)

0 commit comments

Comments
 (0)