summaryrefslogtreecommitdiff
path: root/python/collaboration/group.py
blob: d1fbd9d099b4e10631121456012bcca61ceae6c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import ctypes
from typing import List, Tuple

from .. import _binaryninjacore as core
from . import remote, util


class Group:
	"""
	Class representing a remote Group
	"""
	def __init__(self, handle: core.BNCollaborationGroupHandle):
		self._handle = ctypes.cast(handle, core.BNCollaborationGroupHandle)

	def __del__(self):
		core.BNFreeCollaborationGroup(self._handle)

	def __eq__(self, other):
		if not isinstance(other, Group):
			return False
		return self.id == other.id

	@property
	def remote(self) -> 'remote.Remote':
		"""
		Owning Remote

		:return: Remote object
		"""
		value = core.BNCollaborationGroupGetRemote(self._handle)
		if value is None:
			raise RuntimeError(util._last_error())
		return remote.Remote(handle=value)

	@property
	def url(self) -> str:
		"""
		Web api endpoint url

		:return: URL string
		"""
		return core.BNCollaborationGroupGetUrl(self._handle)

	@property
	def id(self) -> int:
		"""
		Unique id

		:return: Id number
		"""
		return core.BNCollaborationGroupGetId(self._handle)

	@property
	def name(self) -> str:
		"""
		Group name

		:return: Name string
		"""
		return core.BNCollaborationGroupGetName(self._handle)

	@name.setter
	def name(self, name: str):
		"""
		Set group name
		You will need to push the group to update the Remote.

		:param name: New group name
		"""
		core.BNCollaborationGroupSetName(self._handle, name)

	@property
	def users(self) -> List[Tuple[str, str]]:
		"""
		Get list of users in the group

		:return: List of (userid, username) pairs
		"""
		count = ctypes.c_size_t()
		user_ids = ctypes.POINTER(ctypes.c_char_p)()
		usernames = ctypes.POINTER(ctypes.c_char_p)()
		if not core.BNCollaborationGroupGetUsers(self._handle, user_ids, usernames, count):
			raise RuntimeError(util._last_error())
		result = []
		for i in range(count.value):
			result.append((core.pyNativeStr(user_ids[i]), core.pyNativeStr(usernames[i])))
		core.BNFreeStringList(user_ids, count.value)
		core.BNFreeStringList(usernames, count.value)
		return result

	@users.setter
	def users(self, usernames: List[str]):
		"""
		Set the list of users in a group by their usernames.
		You will need to push the group to update the Remote.

		:param usernames: Usernames of new group members
		"""
		array = (ctypes.c_char_p * len(usernames))()
		for i in range(len(usernames)):
			array[i] = core.cstr(usernames[i])
		if not core.BNCollaborationGroupSetUsernames(self._handle, array, len(usernames)):
			raise RuntimeError(util._last_error())

	def contains_user(self, username: str) -> bool:
		"""
		Test if a group has a user with the given username

		:param username: Username of user to check membership
		:return: If the user is in the group
		"""
		return core.BNCollaborationGroupContainsUser(self._handle, username)