Skip to content

Commit b3aa200

Browse files
committed
channel: Add selfHasContentAccess, using canSubscribeGroup et al
Fixes zulip#814. The general implementation of group-based permissions was added in the commits leading up to this one. This commit demonstrates implementing a given such permission. This particular permission is one we don't yet have any logic that should consume; we'll be adding that as part of zulip#188, where we'll want to know whether to offer the option to subscribe to a given channel. To exercise this method on real data, I added some debugging prints that printed a list of the channels where this returned true, and a list of the channels where it returned false.
1 parent 2da050d commit b3aa200

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

lib/model/channel.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
55
import '../api/model/events.dart';
66
import '../api/model/initial_snapshot.dart';
77
import '../api/model/model.dart';
8+
import 'realm.dart';
89
import 'store.dart';
910
import 'user.dart';
1011

@@ -139,6 +140,40 @@ mixin ChannelStore on UserStore {
139140
}
140141
}
141142

143+
bool selfHasContentAccess(ZulipStream channel) {
144+
// Compare web's stream_data.has_content_access.
145+
if (channel.isWebPublic) return true;
146+
if (channel is Subscription) return true;
147+
// Here web calls has_metadata_access... but that always returns true,
148+
// as its comment says.
149+
if (selfUser.role == UserRole.guest) return false;
150+
if (!channel.inviteOnly) return true;
151+
return _selfHasContentAccessViaGroupPermissions(channel);
152+
}
153+
154+
bool _selfHasContentAccessViaGroupPermissions(ZulipStream channel) {
155+
// Compare web's stream_data.has_content_access_via_group_permissions.
156+
if (channel.canAddSubscribersGroup != null
157+
&& selfHasPermissionForGroupSetting(channel.canAddSubscribersGroup!,
158+
GroupSettingType.stream, 'can_add_subscribers_group')) {
159+
// The behavior before this permission was introduced was equivalent to
160+
// the "nobody" group.
161+
// TODO(server-10): simplify
162+
return true;
163+
}
164+
165+
if (channel.canSubscribeGroup != null
166+
&& selfHasPermissionForGroupSetting(channel.canSubscribeGroup!,
167+
GroupSettingType.stream, 'can_subscribe_group')) {
168+
// The behavior before this permission was introduced was equivalent to
169+
// the "nobody" group.
170+
// TODO(server-10): simplify
171+
return true;
172+
}
173+
174+
return false;
175+
}
176+
142177
bool hasPostingPermission({
143178
required ZulipStream inChannel,
144179
required User user,

0 commit comments

Comments
 (0)