Implement setting group permissions

This commit is contained in:
AsamK 2021-05-15 16:37:52 +02:00
parent 7170a68571
commit 78f22c7020
7 changed files with 104 additions and 13 deletions

View file

@ -25,6 +25,7 @@ import org.asamk.signal.manager.groups.GroupIdV1;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupPermission;
import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.manager.helper.GroupV2Helper;
@ -836,6 +837,8 @@ public class Manager implements Closeable {
List<String> removeAdmins,
boolean resetGroupLink,
GroupLinkState groupLinkState,
GroupPermission addMemberPermission,
GroupPermission editDetailsPermission,
File avatarFile,
Integer expirationTimer
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
@ -848,6 +851,8 @@ public class Manager implements Closeable {
removeAdmins == null ? null : getSignalServiceAddresses(removeAdmins),
resetGroupLink,
groupLinkState,
addMemberPermission,
editDetailsPermission,
avatarFile,
expirationTimer);
}
@ -862,8 +867,10 @@ public class Manager implements Closeable {
final Set<RecipientId> removeAdmins,
final boolean resetGroupLink,
final GroupLinkState groupLinkState,
final GroupPermission addMemberPermission,
final GroupPermission editDetailsPermission,
final File avatarFile,
Integer expirationTimer
final Integer expirationTimer
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
var group = getGroupForUpdating(groupId);
@ -877,6 +884,8 @@ public class Manager implements Closeable {
removeAdmins,
resetGroupLink,
groupLinkState,
addMemberPermission,
editDetailsPermission,
avatarFile,
expirationTimer);
}
@ -948,6 +957,8 @@ public class Manager implements Closeable {
final Set<RecipientId> removeAdmins,
final boolean resetGroupLink,
final GroupLinkState groupLinkState,
final GroupPermission addMemberPermission,
final GroupPermission editDetailsPermission,
final File avatarFile,
Integer expirationTimer
) throws IOException {
@ -1020,6 +1031,16 @@ public class Manager implements Closeable {
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (addMemberPermission != null) {
var groupGroupChangePair = groupV2Helper.setAddMemberPermission(group, addMemberPermission);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (editDetailsPermission != null) {
var groupGroupChangePair = groupV2Helper.setEditDetailsPermission(group, editDetailsPermission);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
}
if (expirationTimer != null) {
var groupGroupChangePair = groupV2Helper.setMessageExpirationTimer(group, expirationTimer);
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());

View file

@ -0,0 +1,6 @@
package org.asamk.signal.manager.groups;
public enum GroupPermission {
EVERY_MEMBER,
ONLY_ADMINS,
}

View file

@ -4,6 +4,7 @@ import com.google.protobuf.InvalidProtocolBufferException;
import org.asamk.signal.manager.groups.GroupLinkPassword;
import org.asamk.signal.manager.groups.GroupLinkState;
import org.asamk.signal.manager.groups.GroupPermission;
import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.manager.storage.groups.GroupInfoV2;
import org.asamk.signal.manager.storage.recipients.Profile;
@ -314,6 +315,26 @@ public class GroupV2Helper {
return commitChange(groupInfoV2, change);
}
public Pair<DecryptedGroup, GroupChange> setEditDetailsPermission(
GroupInfoV2 groupInfoV2, GroupPermission permission
) throws IOException {
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);
final var accessRequired = toAccessControl(permission);
final var change = groupOperations.createChangeAttributesRights(accessRequired);
return commitChange(groupInfoV2, change);
}
public Pair<DecryptedGroup, GroupChange> setAddMemberPermission(
GroupInfoV2 groupInfoV2, GroupPermission permission
) throws IOException {
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);
final var accessRequired = toAccessControl(permission);
final var change = groupOperations.createChangeMembershipRights(accessRequired);
return commitChange(groupInfoV2, change);
}
public GroupChange joinGroup(
GroupMasterKey groupMasterKey,
GroupLinkPassword groupLinkPassword,
@ -390,6 +411,17 @@ public class GroupV2Helper {
}
}
private AccessControl.AccessRequired toAccessControl(final GroupPermission permission) {
switch (permission) {
case EVERY_MEMBER:
return AccessControl.AccessRequired.MEMBER;
case ONLY_ADMINS:
return AccessControl.AccessRequired.ADMINISTRATOR;
default:
throw new AssertionError();
}
}
private GroupsV2Operations.GroupOperations getGroupOperations(final GroupInfoV2 groupInfoV2) {
final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupInfoV2.getMasterKey());
return groupsV2Operations.forGroup(groupSecretParams);