CCLogin.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //
  2. // CCLogin.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 09/04/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCLogin.h"
  24. #import "AppDelegate.h"
  25. #import "CCUtility.h"
  26. #import "NCBridgeSwift.h"
  27. #import "NCNetworkingEndToEnd.h"
  28. @interface CCLogin () <CCLoginDelegateWeb>
  29. {
  30. AppDelegate *appDelegate;
  31. UIView *rootView;
  32. }
  33. @end
  34. @implementation CCLogin
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  39. // Background color
  40. self.view.backgroundColor = [NCBrandColor sharedInstance].customer;
  41. // Image Brand
  42. self.imageBrand.image = [UIImage imageNamed:@"loginLogo"];
  43. // Annulla
  44. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  45. self.annulla.tintColor = [NCBrandColor sharedInstance].customerText;
  46. // Base URL
  47. _imageBaseUrl.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginURL"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  48. _baseUrl.textColor = [NCBrandColor sharedInstance].customerText;
  49. _baseUrl.tintColor = [NCBrandColor sharedInstance].customerText;
  50. _baseUrl.placeholder = NSLocalizedString(@"_login_url_", nil);
  51. [_baseUrl setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  52. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  53. [self.baseUrl setDelegate:self];
  54. // Loading Base Utl GIF
  55. self.loadingBaseUrl.image = [UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource: @"loading@2x" withExtension:@"gif"]];
  56. self.loadingBaseUrl.hidden = YES;
  57. // User
  58. _imageUser.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginUser"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  59. _user.textColor = [NCBrandColor sharedInstance].customerText;
  60. _user.tintColor = [NCBrandColor sharedInstance].customerText;
  61. _user.placeholder = NSLocalizedString(@"_username_", nil);
  62. [_user setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  63. [self.user setFont:[UIFont systemFontOfSize:13]];
  64. [self.user setDelegate:self];
  65. // Password
  66. _imagePassword.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginPassword"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  67. _password.textColor = [NCBrandColor sharedInstance].customerText;
  68. _password.tintColor = [NCBrandColor sharedInstance].customerText;
  69. _password.placeholder = NSLocalizedString(@"_password_", nil);
  70. [_password setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  71. [self.password setFont:[UIFont systemFontOfSize:13]];
  72. [self.password setDelegate:self];
  73. // Login
  74. [self.login setTitle:[NSLocalizedString(@"_login_", nil) uppercaseString] forState:UIControlStateNormal] ;
  75. self.login.backgroundColor = [NCBrandColor sharedInstance].customerText;
  76. self.login.tintColor = [UIColor blackColor];
  77. // self.login.layer.cornerRadius = 3;
  78. // self.login.clipsToBounds = YES;
  79. // Type view
  80. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  81. [self.loginTypeView setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  82. // Preferred providers
  83. self.preferredProviders.text = NSLocalizedString([NCBrandOptions sharedInstance].textLoginProvider, nil);
  84. self.preferredProviders.textColor = [UIColor whiteColor];
  85. self.preferredProviders.userInteractionEnabled = YES;
  86. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider) {
  87. self.preferredProviders.hidden = YES;
  88. }
  89. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginPreferredProviders)];
  90. [self.preferredProviders addGestureRecognizer:tapGesture];
  91. if (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) {
  92. // Portrait
  93. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider == NO)
  94. self.preferredProviders.hidden = NO;
  95. self.loginTypeView.hidden = NO;
  96. } else {
  97. // Landscape
  98. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
  99. self.preferredProviders.hidden = YES;
  100. self.loginTypeView.hidden = YES;
  101. }
  102. }
  103. // Brand
  104. if ([NCBrandOptions sharedInstance].disable_request_login_url) {
  105. _baseUrl.text = [NCBrandOptions sharedInstance].loginBaseUrl;
  106. _imageBaseUrl.hidden = YES;
  107. _baseUrl.hidden = YES;
  108. }
  109. if (_loginType == k_login_Add ) {
  110. // Login Flow ?
  111. _imageUser.hidden = YES;
  112. _user.hidden = YES;
  113. _imagePassword.hidden = YES;
  114. _password.hidden = YES;
  115. }
  116. if (_loginType == k_login_Add_Forced) {
  117. _annulla.hidden = YES;
  118. // Login Flow ?
  119. _imageUser.hidden = YES;
  120. _user.hidden = YES;
  121. _imagePassword.hidden = YES;
  122. _password.hidden = YES;
  123. }
  124. if (_loginType == k_login_Modify_Password) {
  125. _baseUrl.text = appDelegate.activeUrl;
  126. _baseUrl.userInteractionEnabled = NO;
  127. _baseUrl.textColor = [UIColor lightGrayColor];
  128. _user.text = appDelegate.activeUser;
  129. _user.userInteractionEnabled = NO;
  130. _user.textColor = [UIColor lightGrayColor];
  131. }
  132. }
  133. - (void)viewWillAppear:(BOOL)animated
  134. {
  135. [super viewWillAppear:animated];
  136. // verify URL
  137. if (_loginType == k_login_Modify_Password && [self.baseUrl.text length] > 0)
  138. [self testUrl];
  139. }
  140. // E' apparsa
  141. - (void)viewDidAppear:(BOOL)animated
  142. {
  143. [super viewDidAppear:animated];
  144. }
  145. //
  146. - (void)viewDidDisappear:(BOOL)animated
  147. {
  148. [super viewDidDisappear:animated];
  149. }
  150. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  151. {
  152. [textField resignFirstResponder];
  153. return YES;
  154. }
  155. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  156. {
  157. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  158. if (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) {
  159. // Portrait
  160. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider == NO)
  161. self.preferredProviders.hidden = NO;
  162. self.loginTypeView.hidden = NO;
  163. } else {
  164. // Landscape
  165. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
  166. self.preferredProviders.hidden = YES;
  167. self.loginTypeView.hidden = YES;
  168. }
  169. }
  170. }];
  171. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  172. }
  173. #pragma --------------------------------------------------------------------------------------------
  174. #pragma mark == Chech Server URL ==
  175. #pragma --------------------------------------------------------------------------------------------
  176. - (void)testUrl
  177. {
  178. self.login.enabled = NO;
  179. self.loadingBaseUrl.hidden = NO;
  180. // Check whether baseUrl contain protocol. If not add https:// by default.
  181. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  182. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  183. }
  184. // Remove trailing slash
  185. if ([self.baseUrl.text hasSuffix:@"/"])
  186. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  187. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:@"" withUserID:@"" withPassword:@"" withUrl:nil];
  188. [ocNetworking serverStatus:self.baseUrl.text success:^(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor) {
  189. self.loadingBaseUrl.hidden = YES;
  190. self.login.enabled = YES;
  191. // Login Flow
  192. if (_user.hidden && _password.hidden && versionMajor >= k_flow_version_available) {
  193. appDelegate.activeLoginWeb = [CCLoginWeb new];
  194. appDelegate.activeLoginWeb.loginType = _loginType;
  195. appDelegate.activeLoginWeb.delegate = self;
  196. appDelegate.activeLoginWeb.urlBase = self.baseUrl.text;
  197. [appDelegate.activeLoginWeb presentModalWithDefaultTheme:self];
  198. }
  199. // NO Login Flow available
  200. if (versionMajor < k_flow_version_available) {
  201. [self.loginTypeView setHidden:YES];
  202. _imageUser.hidden = NO;
  203. _user.hidden = NO;
  204. _imagePassword.hidden = NO;
  205. _password.hidden = NO;
  206. [_user becomeFirstResponder];
  207. }
  208. } failure:^(NSString *message, NSInteger errorCode) {
  209. self.loadingBaseUrl.hidden = YES;
  210. self.login.enabled = YES;
  211. if (errorCode == NSURLErrorServerCertificateUntrusted) {
  212. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:message viewController:self delegate:self];
  213. } else {
  214. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  215. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  216. [alertController addAction:okAction];
  217. [self presentViewController:alertController animated:YES completion:nil];
  218. }
  219. }];
  220. }
  221. - (void)trustedCerticateAccepted
  222. {
  223. NSLog(@"[LOG] Certificate trusted");
  224. }
  225. - (void)trustedCerticateDenied
  226. {
  227. if (_loginType == k_login_Modify_Password)
  228. [self handleAnnulla:self];
  229. }
  230. #pragma --------------------------------------------------------------------------------------------
  231. #pragma mark == TextField ==
  232. #pragma --------------------------------------------------------------------------------------------
  233. -(void)textFieldDidBeginEditing:(UITextField *)textField
  234. {
  235. if (textField == self.password) {
  236. self.toggleVisiblePassword.hidden = NO;
  237. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  238. }
  239. }
  240. -(void)textFieldDidEndEditing:(UITextField *)textField
  241. {
  242. if (textField == self.password) {
  243. self.toggleVisiblePassword.hidden = YES;
  244. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  245. }
  246. }
  247. #pragma --------------------------------------------------------------------------------------------
  248. #pragma mark === CCLoginDelegateWeb ===
  249. #pragma --------------------------------------------------------------------------------------------
  250. - (void)loginSuccess:(NSInteger)loginType
  251. {
  252. [self.delegate loginSuccess:_loginType];
  253. }
  254. - (void)webDismiss
  255. {
  256. [self dismissViewControllerAnimated:YES completion:nil];
  257. }
  258. #pragma --------------------------------------------------------------------------------------------
  259. #pragma mark == Action ==
  260. #pragma --------------------------------------------------------------------------------------------
  261. - (void)loginPreferredProviders
  262. {
  263. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NCBrandOptions sharedInstance].linkLoginProvider]];
  264. appDelegate.activeLoginWeb = [CCLoginWeb new];
  265. appDelegate.activeLoginWeb.loginType = _loginType;
  266. appDelegate.activeLoginWeb.delegate = self;
  267. appDelegate.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] loginPreferredProviders];
  268. [appDelegate.activeLoginWeb presentModalWithDefaultTheme:self];
  269. }
  270. - (IBAction)handlebaseUrlchange:(id)sender
  271. {
  272. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  273. [self performSelector:@selector(testUrl) withObject:nil];
  274. }
  275. - (IBAction)handleButtonLogin:(id)sender
  276. {
  277. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  278. [self testUrl];
  279. return;
  280. }
  281. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  282. // remove last char if /
  283. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  284. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  285. NSString *url = self.baseUrl.text;
  286. NSString *user = self.user.text;
  287. NSString *password = self.password.text;
  288. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:user withUserID:user withPassword:password withUrl:nil];
  289. self.login.enabled = NO;
  290. self.loadingBaseUrl.hidden = NO;
  291. [ocNetworking checkServer:[NSString stringWithFormat:@"%@%@", url, k_webDAV] success:^{
  292. // account
  293. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  294. if (_loginType == k_login_Modify_Password) {
  295. // Change Password
  296. tableAccount *tbAccount = [[NCManageDatabase sharedInstance] setAccountPassword:account password:password];
  297. // Setting appDelegate active account
  298. [appDelegate settingActiveAccount:tbAccount.account activeUrl:tbAccount.url activeUser:tbAccount.user activeUserID:tbAccount.userID activePassword:tbAccount.password];
  299. [self.delegate loginSuccess:_loginType];
  300. [self dismissViewControllerAnimated:YES completion:nil];
  301. } else {
  302. // LOGOUT
  303. [appDelegate unsubscribingNextcloudServerPushNotification];
  304. [[NCManageDatabase sharedInstance] deleteAccount:account];
  305. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:password loginFlow:false];
  306. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  307. // Setting appDelegate active account
  308. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:tableAccount.password];
  309. [self.delegate loginSuccess:_loginType];
  310. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  311. [self dismissViewControllerAnimated:YES completion:nil];
  312. });
  313. }
  314. } failure:^(NSString *message, NSInteger errorCode) {
  315. if (errorCode != NSURLErrorServerCertificateUntrusted) {
  316. NSString *messageAlert = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedStringFromTable(@"_not_possible_connect_to_server_", @"Error", nil), message];
  317. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:messageAlert preferredStyle:UIAlertControllerStyleAlert];
  318. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  319. [alertController addAction:okAction];
  320. [self presentViewController:alertController animated:YES completion:nil];
  321. }
  322. self.login.enabled = YES;
  323. self.loadingBaseUrl.hidden = YES;
  324. }];
  325. }
  326. }
  327. - (IBAction)handleAnnulla:(id)sender
  328. {
  329. [self dismissViewControllerAnimated:YES completion:nil];
  330. }
  331. - (IBAction)handleToggleVisiblePassword:(id)sender
  332. {
  333. NSString *currentPassword = self.password.text;
  334. self.password.secureTextEntry = ! self.password.secureTextEntry;
  335. self.password.text = @"";
  336. self.password.text = currentPassword;
  337. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [NCBrandColor sharedInstance].customerText};
  338. }
  339. - (IBAction)handleLoginTypeView:(id)sender
  340. {
  341. if (_user.hidden && _password.hidden) {
  342. _imageUser.hidden = NO;
  343. _user.hidden = NO;
  344. _imagePassword.hidden = NO;
  345. _password.hidden = NO;
  346. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  347. } else {
  348. _imageUser.hidden = YES;
  349. _user.hidden = YES;
  350. _imagePassword.hidden = YES;
  351. _password.hidden = YES;
  352. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  353. }
  354. }
  355. @end