1+ using StreamChat . Clients ;
2+ using StreamChat . Models ;
3+
4+ namespace DocsExamples ;
5+
6+ /// <summary>
7+ /// Code examples for <see href="https://getstream.io/chat/docs/python/user_permissions/"/>
8+ /// </summary>
9+ internal class UserPermissions
10+ {
11+ private readonly IUserClient _userClient ;
12+ private readonly IChannelClient _channelClient ;
13+ private readonly IPermissionClient _permissionClient ;
14+ private readonly IChannelTypeClient _channelTypeClient ;
15+ private readonly IAppClient _appClient ;
16+
17+ public UserPermissions ( )
18+ {
19+ var factory = new StreamClientFactory ( "{{ api_key }}" , "{{ api_secret }}" ) ;
20+ _userClient = factory . GetUserClient ( ) ;
21+ _channelClient = factory . GetChannelClient ( ) ;
22+ _permissionClient = factory . GetPermissionClient ( ) ;
23+ _channelTypeClient = factory . GetChannelTypeClient ( ) ;
24+ _appClient = factory . GetAppClient ( ) ;
25+ }
26+
27+ internal async Task ChangeUserRole ( )
28+ {
29+ var upsertResponse = await _userClient . UpdatePartialAsync ( new UserPartialRequest
30+ {
31+ Id = "user-id" ,
32+ Set = new Dictionary < string , object >
33+ {
34+ { "role" , "special_agent" }
35+ }
36+ } ) ;
37+ }
38+
39+ internal async Task VerifyChannelMemberRoleAssigned ( )
40+ {
41+ var addMembersResponse
42+ = await _channelClient . AddMembersAsync ( "channel-type" , "channel-id" , new [ ] { "user-id" } ) ;
43+ Console . WriteLine ( addMembersResponse . Members [ 0 ] . ChannelRole ) ; // channel role is equal to "channel_member"
44+ }
45+
46+ internal async Task AssignRoles ( )
47+ {
48+ // User must be a member of the channel before you can assign channel role
49+ var resp = await _channelClient . AssignRolesAsync ( "channel-type" , "channel-id" , new AssignRoleRequest
50+ {
51+ AssignRoles = new List < RoleAssignment >
52+ {
53+ new RoleAssignment { UserId = "user-id" , ChannelRole = Role . ChannelModerator }
54+ }
55+ } ) ;
56+ }
57+
58+ internal async Task CreateRole ( )
59+ {
60+ await _permissionClient . CreateRoleAsync ( "special_agent" ) ;
61+ }
62+
63+ internal async Task DeleteRole ( )
64+ {
65+ await _permissionClient . DeleteRoleAsync ( "special_agent" ) ;
66+ }
67+
68+ internal async Task ListPermissions ( )
69+ {
70+ var response = await _permissionClient . ListPermissionsAsync ( ) ;
71+ }
72+
73+ internal async Task UpdateGrantedPermissions ( )
74+ {
75+ // observe current grants of the channel type
76+ var channelType = await _channelTypeClient . GetChannelTypeAsync ( "messaging" ) ;
77+ Console . WriteLine ( channelType . Grants ) ;
78+
79+ // update "channel_member" role grants in "messaging" scope
80+ var update = new ChannelTypeWithStringCommandsRequest
81+ {
82+ Grants = new Dictionary < string , List < string > >
83+ {
84+ {
85+ // This will replace all existing grants of "channel_member" role
86+ "channel_member" , new List < string >
87+ {
88+ "read-channel" , // allow access to the channel
89+ "create-message" , // create messages in the channel
90+ "update-message-owner" , // update own user messages
91+ "delete-message-owner" , // delete own user messages
92+ }
93+ } ,
94+ }
95+ } ;
96+ await _channelTypeClient . UpdateChannelTypeAsync ( "messaging" , update ) ;
97+ }
98+
99+ internal async Task RemoveGrantedPermissionsByCategory ( )
100+ {
101+ var update = new ChannelTypeWithStringCommandsRequest
102+ {
103+ Grants = new Dictionary < string , List < string > >
104+ {
105+ { "guest" , new List < string > ( ) } , // removes all grants of "guest" role
106+ { "anonymous" , new List < string > ( ) } , // removes all grants of "anonymous" role
107+ }
108+ } ;
109+ await _channelTypeClient . UpdateChannelTypeAsync ( "messaging" , update ) ;
110+ }
111+
112+ internal async Task ResetGrantsToDefaultSettings ( )
113+ {
114+ var update = new ChannelTypeWithStringCommandsRequest
115+ {
116+ Grants = new Dictionary < string , List < string > > ( )
117+ } ;
118+ await _channelTypeClient . UpdateChannelTypeAsync ( "messaging" , update ) ;
119+ }
120+
121+ internal async Task UpdateAppScopedGrants ( )
122+ {
123+ var settings = new AppSettingsRequest
124+ {
125+ Grants = new Dictionary < string , List < string > >
126+ {
127+ { "anonymous" , new List < string > ( ) } ,
128+ { "guest" , new List < string > ( ) } ,
129+ { "user" , new List < string > { "search-user" , "mute-user" } } ,
130+ { "admin" , new List < string > { "search-user" , "mute-user" , "ban-user" } } ,
131+ }
132+ } ;
133+ await _appClient . UpdateAppSettingsAsync ( settings ) ;
134+ }
135+
136+ internal async Task UpdateChannelLevelPermissions ( )
137+ {
138+ var grants = new Dictionary < string , object > { { "user" , new List < string > { "!add-links" , "create-reaction" } } } ;
139+ var overrides = new Dictionary < string , object > { { "grants" , grants } } ;
140+ var request = new PartialUpdateChannelRequest
141+ {
142+ Set = new Dictionary < string , object >
143+ {
144+ { "config_overrides" , overrides }
145+ }
146+ } ;
147+ var resp = await _channelClient . PartialUpdateAsync ( "channel-type" , "channel-id" , request ) ;
148+ }
149+ }
0 commit comments