PermissionChecker.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "PermissionChecker.h"
  21. #import "UserSettings.h"
  22. #import "MyIdentityStore.h"
  23. #import "Contact.h"
  24. #import "Group.h"
  25. #import "EntityManager.h"
  26. @interface PermissionChecker ()
  27. @property UIViewController *viewController;
  28. @end
  29. @implementation PermissionChecker
  30. + (instancetype)permissionChecker {
  31. PermissionChecker *permissionChecker = [[PermissionChecker alloc] init];
  32. return permissionChecker;
  33. }
  34. + (instancetype)permissionCheckerPresentingAlarmsOn:(UIViewController *)viewController {
  35. PermissionChecker *permissionChecker = [PermissionChecker permissionChecker];
  36. permissionChecker.viewController = viewController;
  37. return permissionChecker;
  38. }
  39. - (BOOL)canSendIn:(Conversation *)conversation entityManager:(EntityManager *)entityManager {
  40. EntityManager *manager = entityManager;
  41. if (manager == nil) {
  42. manager = [[EntityManager alloc] init];
  43. }
  44. // Check for blacklisted contact
  45. if (conversation.groupId == nil && [[UserSettings sharedUserSettings].blacklist containsObject:conversation.contact.identity]) {
  46. if (_viewController) {
  47. [self showAlert:NSLocalizedString(@"contact_blocked_cannot_send", nil)];
  48. }
  49. return NO;
  50. }
  51. // Check for empty groups
  52. if (conversation.groupId != nil && conversation.members.count == 0) {
  53. if (_viewController) {
  54. [self showAlert:NSLocalizedString(@"no_more_members", nil)];
  55. }
  56. return NO;
  57. }
  58. // Check that the group was started while we were using the same identity as now
  59. if (conversation.groupMyIdentity != nil && ![conversation.groupMyIdentity isEqualToString:[MyIdentityStore sharedMyIdentityStore].identity]) {
  60. if (_viewController) {
  61. [self showAlert:NSLocalizedString(@"group_different_identity", nil)];
  62. }
  63. return NO;
  64. }
  65. // Check for invalid contact
  66. if (conversation.groupId == nil && conversation.contact.state.intValue == kStateInvalid) {
  67. if (_viewController) {
  68. [self showAlert:NSLocalizedString(@"contact_invalid_cannot_send", nil)];
  69. }
  70. return NO;
  71. }
  72. // Check group state
  73. if (conversation.isGroup) {
  74. Group *group = [manager.entityFetcher groupForConversation:conversation];
  75. if (group.didLeave || group.didForcedLeave) {
  76. if (_viewController) {
  77. [self showAlert:NSLocalizedString(@"group_is_not_member", nil)];
  78. }
  79. return NO;
  80. }
  81. }
  82. return YES;
  83. }
  84. - (void)showAlert:(NSString *)title {
  85. [UIAlertTemplate showAlertWithOwner:_viewController title:title message:@"" actionOk:nil];
  86. }
  87. @end