From 4b1ca0ff7ff6d17f1baf896b05573396eb62f3c7 Mon Sep 17 00:00:00 2001 From: Frank Rotermund Date: Tue, 22 Mar 2022 14:37:58 +0000 Subject: [PATCH] Feature/2897 restricted user --- Config/BuildSettings.swift | 4 +- Riot/Assets/de.lproj/Bwi.strings | 3 + Riot/Assets/en.lproj/Bwi.strings | 3 + .../Common/Recents/RecentsViewController.m | 8 ++- .../UnifiedSearchViewController.m | 14 ++-- .../Members/RoomParticipantsViewController.m | 4 +- .../ShowDirectoryViewModel.swift | 5 ++ .../StartChat/StartChatViewController.m | 6 +- RiotNSE/target.yml | 1 + RiotShareExtension/target.yml | 1 + .../AccountRestriction.swift | 29 ++++++++ .../AccountRestrictionService.swift | 69 +++++++++++++++++++ .../DeveloperSettingsView.swift | 27 ++++++++ bwi/ServerURLs/ServerURLHelper.swift | 18 +++-- bwi/ServerURLs/ServerURLs.swift | 1 + bwi/UserAgent/UserAgentService.swift | 6 +- 16 files changed, 183 insertions(+), 16 deletions(-) create mode 100644 bwi/AccountRestriction/AccountRestriction.swift create mode 100644 bwi/AccountRestriction/AccountRestrictionService.swift diff --git a/Config/BuildSettings.swift b/Config/BuildSettings.swift index 9148fd4aa..f20fbeeff 100644 --- a/Config/BuildSettings.swift +++ b/Config/BuildSettings.swift @@ -462,7 +462,7 @@ final class BuildSettings: NSObject { static let customServerDisplayName : String = "" // MARK BWI show/hide developer menu - static let bwiShowDeveloperSettings : Bool = false + static let bwiShowDeveloperSettings : Bool = true // MARK BWI personal notes room static let bwiPersonalNotesRoom : Bool = true @@ -512,7 +512,7 @@ final class BuildSettings: NSObject { // MARK: - Location Sharing static let tileServerMapURL = "" - static let tileServerMapStyleURL = URL(string: "")! + static let tileServerMapStyleURL = URL(string: "https://map.tyler.org")! static let locationSharingEnabled = false } diff --git a/Riot/Assets/de.lproj/Bwi.strings b/Riot/Assets/de.lproj/Bwi.strings index 475990c21..1a6936fea 100644 --- a/Riot/Assets/de.lproj/Bwi.strings +++ b/Riot/Assets/de.lproj/Bwi.strings @@ -29,4 +29,7 @@ "bwi_settings_developer_apply_orig_app_config" = "MDM Config: BW"; "bwi_settings_developer_reset_app_config" = "MDM Config: Reset"; +"bwi_settings_developer_restrict_user" = "Nutzer einschränken"; +"bwi_settings_developer_unrestrict_user" = "Nutzereinschränkung aufheben"; + "bwi_mdm_logout_message" = "Die Konfiguration hat sich geändert. Bitte melde dich neu an."; diff --git a/Riot/Assets/en.lproj/Bwi.strings b/Riot/Assets/en.lproj/Bwi.strings index 264b333fe..65027bebc 100644 --- a/Riot/Assets/en.lproj/Bwi.strings +++ b/Riot/Assets/en.lproj/Bwi.strings @@ -29,4 +29,7 @@ "bwi_settings_developer_apply_orig_app_config" = "MDM Config: BW"; "bwi_settings_developer_reset_app_config" = "MDM Config: Reset"; +"bwi_settings_developer_restrict_user" = "Restrict user"; +"bwi_settings_developer_unrestrict_user" = "Remove user restriction"; + "bwi_mdm_logout_message" = "The configuration has changed. Please reconnect."; diff --git a/Riot/Modules/Common/Recents/RecentsViewController.m b/Riot/Modules/Common/Recents/RecentsViewController.m index a6a3a501e..3787717c0 100644 --- a/Riot/Modules/Common/Recents/RecentsViewController.m +++ b/Riot/Modules/Common/Recents/RecentsViewController.m @@ -859,8 +859,12 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro // Sanity check if (self.parentViewController && [self.parentViewController isKindOfClass:UnifiedSearchViewController.class]) { - // Show the directory screen - [((UnifiedSearchViewController*)self.parentViewController) showPublicRoomsDirectory]; + AccountRestrictionService *service = [[AccountRestrictionService alloc] initWithMxSession:self.mainSession]; + + if (![service isRoomAccessRestriction]) { + // Show the directory screen + [((UnifiedSearchViewController*)self.parentViewController) showPublicRoomsDirectory]; + } } } diff --git a/Riot/Modules/GlobalSearch/UnifiedSearchViewController.m b/Riot/Modules/GlobalSearch/UnifiedSearchViewController.m index aae50f8c8..453f81cd6 100644 --- a/Riot/Modules/GlobalSearch/UnifiedSearchViewController.m +++ b/Riot/Modules/GlobalSearch/UnifiedSearchViewController.m @@ -538,15 +538,21 @@ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { + AccountRestrictionService* service = [[AccountRestrictionService alloc] initWithMxSession:self.mainSession]; + if (self.selectedViewController == recentsViewController) { - // As the public room search is local, it can be updated on each text change - [self updateSearch]; + if (![service isRoomAccessRestriction]) { + // As the public room search is local, it can be updated on each text change + [self updateSearch]; + } } else if (self.selectedViewController == peopleSearchViewController) { - // As the contact search is local, it can be updated on each text change - [self updateSearch]; + if (![service isAddressListRestriction]) { + // As the contact search is local, it can be updated on each text change + [self updateSearch]; + } } else if (!self.searchBar.text.length) { diff --git a/Riot/Modules/Room/Members/RoomParticipantsViewController.m b/Riot/Modules/Room/Members/RoomParticipantsViewController.m index 125e6b348..2bce57114 100644 --- a/Riot/Modules/Room/Members/RoomParticipantsViewController.m +++ b/Riot/Modules/Room/Members/RoomParticipantsViewController.m @@ -525,7 +525,9 @@ - (void)updateInviteButtonForRoom:(MXRoom *)room { - if ((room.isDirect && !BwiBuildSettings.allowInviteOnDirectRooms) || room.isPersonalNotesRoom) + AccountRestrictionService *service = [[AccountRestrictionService alloc] initWithMxSession:room.mxSession]; + + if ((room.isDirect && !BwiBuildSettings.allowInviteOnDirectRooms) || room.isPersonalNotesRoom || [service isAddressListRestriction]) { // Remove invite members button if exists [self->inviteFabImageView removeFromSuperview]; diff --git a/Riot/Modules/Rooms/ShowDirectory/ShowDirectoryViewModel.swift b/Riot/Modules/Rooms/ShowDirectory/ShowDirectoryViewModel.swift index 552b27963..8b3503f7d 100644 --- a/Riot/Modules/Rooms/ShowDirectory/ShowDirectoryViewModel.swift +++ b/Riot/Modules/Rooms/ShowDirectory/ShowDirectoryViewModel.swift @@ -132,6 +132,11 @@ final class ShowDirectoryViewModel: NSObject, ShowDirectoryViewModelType { return } + if AccountRestrictionService(mxSession: session).isRoomAccessRestriction() { + self.update(viewState: .loadedWithoutUpdate) + return + } + self.update(viewState: .loading) // Useful only when force is true diff --git a/Riot/Modules/StartChat/StartChatViewController.m b/Riot/Modules/StartChat/StartChatViewController.m index ce976f76d..ba1db7115 100644 --- a/Riot/Modules/StartChat/StartChatViewController.m +++ b/Riot/Modules/StartChat/StartChatViewController.m @@ -706,7 +706,11 @@ - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { - [contactsDataSource searchWithPattern:searchText forceReset:NO]; + AccountRestrictionService *service = [[AccountRestrictionService alloc] initWithMxSession:self.mainSession]; + + if(![service isAddressListRestriction]) { + [contactsDataSource searchWithPattern:searchText forceReset:NO]; + } self.contactsAreFilteredWithSearch = searchText.length ? YES : NO; } diff --git a/RiotNSE/target.yml b/RiotNSE/target.yml index 281adc995..c1bbe8c22 100644 --- a/RiotNSE/target.yml +++ b/RiotNSE/target.yml @@ -38,6 +38,7 @@ targets: sources: - path: . - path: ../bwi/AppConfig + - path: ../bwi/ServerURLs - path: ../bwi/SecureStorage - path: ../bwi/BwiBuildSettings.swift - path: ../Config/BwiSettings.swift diff --git a/RiotShareExtension/target.yml b/RiotShareExtension/target.yml index a3606362d..abad71bf6 100644 --- a/RiotShareExtension/target.yml +++ b/RiotShareExtension/target.yml @@ -38,6 +38,7 @@ targets: sources: - path: . - path: ../bwi/SecureStorage + - path: ../bwi/ServerURLs - path: ../bwi/BwiBuildSettings.swift - path: ../Config/BwiSettings.swift - path: ../Riot/Modules/Common/SegmentedViewController/SegmentedViewController.m diff --git a/bwi/AccountRestriction/AccountRestriction.swift b/bwi/AccountRestriction/AccountRestriction.swift new file mode 100644 index 000000000..31c4e26be --- /dev/null +++ b/bwi/AccountRestriction/AccountRestriction.swift @@ -0,0 +1,29 @@ +// +/* + * Copyright (c) 2022 BWI GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation + +struct AccountRestriction: Codable { + + enum CodingKeys: String, CodingKey { + case roomAccess = "can_access_room_directory" + case addressListAccess = "can_access_address_book" + } + + let roomAccess: Bool + let addressListAccess: Bool +} diff --git a/bwi/AccountRestriction/AccountRestrictionService.swift b/bwi/AccountRestriction/AccountRestrictionService.swift new file mode 100644 index 000000000..c1e0cd556 --- /dev/null +++ b/bwi/AccountRestriction/AccountRestrictionService.swift @@ -0,0 +1,69 @@ +// +/* + * Copyright (c) 2022 BWI GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation + +@objcMembers class AccountRestrictionService : NSObject { + + private enum AccountDataTypes { + static let restrictions = "m.bwi.account_restrictions" + } + + let session: MXSession + private lazy var serializationService: SerializationServiceType = SerializationService() + + init(mxSession: MXSession) { + self.session = mxSession + } + + func isRoomAccessRestriction() -> Bool { + guard let restrictionsDict = session.accountData.accountData(forEventType: AccountDataTypes.restrictions) as? [String: Any] else { + return false + } + do { + let restriction: AccountRestriction = try serializationService.deserialize(restrictionsDict) + + return restriction.roomAccess + } catch { + + } + + return false + } + + func isAddressListRestriction() -> Bool { + guard let restrictionsDict = session.accountData.accountData(forEventType: AccountDataTypes.restrictions) as? [String: Any] else { + return false + } + do { + let restriction: AccountRestriction = try serializationService.deserialize(restrictionsDict) + + return restriction.addressListAccess + } catch { + return false + } + } + + func updateRestriction(roomAccess: Bool, addressListAccess: Bool) { + var restrictionDict = session.accountData.accountData(forEventType: AccountDataTypes.restrictions) ?? [:] + + restrictionDict[AccountRestriction.CodingKeys.roomAccess.rawValue] = roomAccess + restrictionDict[AccountRestriction.CodingKeys.addressListAccess.rawValue] = addressListAccess + + session.setAccountData(restrictionDict, forType: AccountDataTypes.restrictions, success: nil, failure: nil) + } +} diff --git a/bwi/DeveloperSettings/DeveloperSettingsView.swift b/bwi/DeveloperSettings/DeveloperSettingsView.swift index 4980e59ff..48ed9e48b 100644 --- a/bwi/DeveloperSettings/DeveloperSettingsView.swift +++ b/bwi/DeveloperSettings/DeveloperSettingsView.swift @@ -51,6 +51,16 @@ struct DeveloperSettingsView: View { .alert(isPresented: $showAlert) { Alert(title: Text(NSLocalizedString("bwi_settings_developer", tableName: "Vector", comment: "")), message: Text(NSLocalizedString("bwi_settings_developer_show_matomo_privacy_notes_resetted", tableName: "Vector", comment: "")), dismissButton: .default(Text("Ok"))) } + Button(action: { _ = restrictUser(mxSession: session) }) { + Text(NSLocalizedString("bwi_settings_developer_restrict_user", tableName: "Bwi", comment: "")) + .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .font(.system(size: 17)) + } + Button(action: { _ = unrestrictUser(mxSession: session) }) { + Text(NSLocalizedString("bwi_settings_developer_unrestrict_user", tableName: "Bwi", comment: "")) + .foregroundColor(Color(ThemeService.shared().theme.tintColor)) + .font(.system(size: 17)) + } } .navigationTitle(NSLocalizedString("bwi_settings_developer", tableName: "Vector", comment: "")) .navigationBarTitleDisplayMode(.inline) @@ -82,3 +92,20 @@ fileprivate func resetMatomoInfoScreen() -> Bool { UserDefaults.standard.set(false, forKey: "bwi_matomo_info_screen_shown") return true } + +fileprivate func restrictUser(mxSession: MXSession?) -> Bool { + guard let mxSession = mxSession else { + return false + } + AccountRestrictionService(mxSession: mxSession).updateRestriction(roomAccess: true, addressListAccess: true) + + return true +} + +fileprivate func unrestrictUser(mxSession: MXSession?) -> Bool { + guard let mxSession = mxSession else { + return false + } + AccountRestrictionService(mxSession: mxSession).updateRestriction(roomAccess: false, addressListAccess: false) + return true +} diff --git a/bwi/ServerURLs/ServerURLHelper.swift b/bwi/ServerURLs/ServerURLHelper.swift index 393216b04..1a22d8f17 100644 --- a/bwi/ServerURLs/ServerURLHelper.swift +++ b/bwi/ServerURLs/ServerURLHelper.swift @@ -20,8 +20,10 @@ import Foundation @objcMembers class ServerURLHelper : NSObject { static let shared = ServerURLHelper() + private let settingsKey = "serverSettings" private let serverUrlKey = "serverURL" private let pusherUrlKey = "pusherURL" + private let flavorUrlKey = "flavor" var serverURLs = Array() @@ -30,14 +32,12 @@ import Foundation do { let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) - if let jsonResult = jsonResult as? Dictionary, let urls = jsonResult["serverURLs"] as? [Any] { + if let jsonResult = jsonResult as? Dictionary, let urls = jsonResult[settingsKey] as? [Any] { for urlSet in urls { - print("SERVERURL: ", urlSet) if let dict = urlSet as? Dictionary { - print("SERVERURL: ", dict) - if let server = dict[serverUrlKey], let pusher = dict[pusherUrlKey] { - serverURLs.append(ServerURLs(serverUrl: server, pusherUrl: pusher)) + if let server = dict[serverUrlKey], let pusher = dict[pusherUrlKey], let flavor = dict[flavorUrlKey] { + serverURLs.append(ServerURLs(serverUrl: server, pusherUrl: pusher, flavor: flavor)) } } @@ -69,4 +69,12 @@ import Foundation return nil } } + + func flavor() -> String? { + if serverURLs.count > 0 { + return serverURLs[0].flavor + } else { + return nil + } + } } diff --git a/bwi/ServerURLs/ServerURLs.swift b/bwi/ServerURLs/ServerURLs.swift index c77f92f88..ac8d1bdb4 100644 --- a/bwi/ServerURLs/ServerURLs.swift +++ b/bwi/ServerURLs/ServerURLs.swift @@ -20,4 +20,5 @@ import Foundation struct ServerURLs { var serverUrl: String var pusherUrl: String + var flavor: String } diff --git a/bwi/UserAgent/UserAgentService.swift b/bwi/UserAgent/UserAgentService.swift index de7d310f1..d60df05c9 100644 --- a/bwi/UserAgent/UserAgentService.swift +++ b/bwi/UserAgent/UserAgentService.swift @@ -66,7 +66,11 @@ import WebKit var bwiUserAgent: String { - let flavor = BwiBuildSettings.flavor + var flavor = BwiBuildSettings.flavor + + if (ServerURLHelper.shared.flavor() != nil) { + flavor = ServerURLHelper.shared.flavor()! + } guard let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName"), let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString"),