CCLogin.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "NCNetworkingSync.h"
  28. @interface CCLogin () <CCLoginDelegateWeb>
  29. {
  30. AppDelegate *appDelegate;
  31. UIView *rootView;
  32. NSString *serverProductName;
  33. NSString *serverVersion;
  34. NSString *serverVersionString;
  35. NSInteger versionMajor;
  36. NSInteger versionMicro;
  37. NSInteger versionMinor;
  38. }
  39. @end
  40. @implementation CCLogin
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  44. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  45. // Background color
  46. self.view.backgroundColor = [NCBrandColor sharedInstance].customer;
  47. // Image Brand
  48. self.imageBrand.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginLogo"] color:[NCBrandColor sharedInstance].customerText];
  49. // Annulla
  50. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  51. self.annulla.tintColor = [NCBrandColor sharedInstance].customerText;
  52. // Base URL
  53. _imageBaseUrl.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginURL"] color:[NCBrandColor sharedInstance].customerText];
  54. _baseUrl.textColor = [NCBrandColor sharedInstance].customerText;
  55. _baseUrl.tintColor = [NCBrandColor sharedInstance].customerText;
  56. _baseUrl.placeholder = NSLocalizedString(@"_login_url_", nil);
  57. [_baseUrl setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  58. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  59. [self.baseUrl setDelegate:self];
  60. // Loading Base Utl GIF
  61. self.loadingBaseUrl.image = [UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource: @"loading@2x" withExtension:@"gif"]];
  62. self.loadingBaseUrl.hidden = YES;
  63. // User
  64. _imageUser.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginUser"] color:[NCBrandColor sharedInstance].customerText];
  65. _user.textColor = [NCBrandColor sharedInstance].customerText;
  66. _user.tintColor = [NCBrandColor sharedInstance].customerText;
  67. _user.placeholder = NSLocalizedString(@"_username_", nil);
  68. [_user setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  69. [self.user setFont:[UIFont systemFontOfSize:13]];
  70. [self.user setDelegate:self];
  71. // Password
  72. _imagePassword.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginPassword"] color:[NCBrandColor sharedInstance].customerText];
  73. _password.textColor = [NCBrandColor sharedInstance].customerText;
  74. _password.tintColor = [NCBrandColor sharedInstance].customerText;
  75. _password.placeholder = NSLocalizedString(@"_password_", nil);
  76. [_password setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
  77. [self.password setFont:[UIFont systemFontOfSize:13]];
  78. [self.password setDelegate:self];
  79. // Login
  80. [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal];
  81. self.login.backgroundColor = [NCBrandColor sharedInstance].customerText;
  82. self.login.tintColor = [NCBrandColor sharedInstance].customer;
  83. // Type view
  84. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  85. [self.loginTypeView setTitleColor:[NCBrandColor sharedInstance].customerText forState:UIControlStateNormal];
  86. // Bottom label
  87. self.bottomLabel.text = NSLocalizedString([NCBrandOptions sharedInstance].textLoginProvider, nil);
  88. self.bottomLabel.userInteractionEnabled = YES;
  89. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider) {
  90. self.bottomLabel.hidden = YES;
  91. }
  92. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabBottomLabel)];
  93. [self.bottomLabel addGestureRecognizer:tapGesture];
  94. 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))) {
  95. // Portrait
  96. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider == NO)
  97. self.bottomLabel.hidden = NO;
  98. self.loginTypeView.hidden = NO;
  99. } else {
  100. // Landscape
  101. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
  102. self.bottomLabel.hidden = YES;
  103. self.loginTypeView.hidden = YES;
  104. }
  105. }
  106. // Brand
  107. if ([NCBrandOptions sharedInstance].disable_request_login_url) {
  108. _baseUrl.text = [NCBrandOptions sharedInstance].loginBaseUrl;
  109. _imageBaseUrl.hidden = YES;
  110. _baseUrl.hidden = YES;
  111. }
  112. if (_loginType == loginAdd) {
  113. // Login Flow ?
  114. _imageUser.hidden = YES;
  115. _user.hidden = YES;
  116. _imagePassword.hidden = YES;
  117. _password.hidden = YES;
  118. }
  119. if (_loginType == loginAddForced) {
  120. _annulla.hidden = YES;
  121. // Login Flow ?
  122. _imageUser.hidden = YES;
  123. _user.hidden = YES;
  124. _imagePassword.hidden = YES;
  125. _password.hidden = YES;
  126. }
  127. if (_loginType == loginModifyPasswordUser) {
  128. _baseUrl.text = appDelegate.activeUrl;
  129. _baseUrl.userInteractionEnabled = NO;
  130. _baseUrl.textColor = [UIColor lightGrayColor];
  131. _user.text = appDelegate.activeUser;
  132. _user.userInteractionEnabled = NO;
  133. _user.textColor = [UIColor lightGrayColor];
  134. }
  135. }
  136. - (void)viewWillAppear:(BOOL)animated
  137. {
  138. [super viewWillAppear:animated];
  139. // verify URL
  140. if (_loginType == loginModifyPasswordUser && [self.baseUrl.text length] > 0)
  141. [self testUrl];
  142. }
  143. // E' apparsa
  144. - (void)viewDidAppear:(BOOL)animated
  145. {
  146. [super viewDidAppear:animated];
  147. }
  148. //
  149. - (void)viewDidDisappear:(BOOL)animated
  150. {
  151. [super viewDidDisappear:animated];
  152. }
  153. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  154. {
  155. [textField resignFirstResponder];
  156. return YES;
  157. }
  158. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  159. {
  160. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  161. 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))) {
  162. // Portrait
  163. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider == NO)
  164. self.bottomLabel.hidden = NO;
  165. self.loginTypeView.hidden = NO;
  166. } else {
  167. // Landscape
  168. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
  169. self.bottomLabel.hidden = YES;
  170. self.loginTypeView.hidden = YES;
  171. }
  172. }
  173. }];
  174. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  175. }
  176. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
  177. {
  178. [super dismissViewControllerAnimated:flag completion:completion];
  179. NSArray *callStack = [NSThread callStackSymbols];
  180. NSString *callParent = [callStack objectAtIndex:1];
  181. if ([callParent containsString:@"CCLogin"])
  182. [self.delegate loginClose];
  183. }
  184. #pragma --------------------------------------------------------------------------------------------
  185. #pragma mark == Chech Server URL ==
  186. #pragma --------------------------------------------------------------------------------------------
  187. - (void)testUrl
  188. {
  189. self.login.enabled = NO;
  190. self.loadingBaseUrl.hidden = NO;
  191. // Check whether baseUrl contain protocol. If not add https:// by default.
  192. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  193. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  194. }
  195. // Remove trailing slash
  196. if ([self.baseUrl.text hasSuffix:@"/"])
  197. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  198. // add status.php for valid test url
  199. NSString *urlTest = [self.baseUrl.text stringByAppendingString:serverStatus];
  200. // Remove stored cookies
  201. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  202. for (NSHTTPCookie *cookie in [storage cookies])
  203. {
  204. [storage deleteCookie:cookie];
  205. }
  206. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest] cachePolicy:0 timeoutInterval:20.0];
  207. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  208. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  209. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  210. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  211. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  212. dispatch_async(dispatch_get_main_queue(), ^{
  213. self.loadingBaseUrl.hidden = YES;
  214. self.login.enabled = YES;
  215. if (error) {
  216. if ([error code] == NSURLErrorServerCertificateUntrusted) {
  217. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:self delegate:self];
  218. } else {
  219. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
  220. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  221. [alertController addAction:okAction];
  222. [self presentViewController:alertController animated:YES completion:nil];
  223. }
  224. } else {
  225. if (![self serverStatus:data]) {
  226. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_serverstatus_error_", nil) message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
  227. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  228. [alertController addAction:okAction];
  229. [self presentViewController:alertController animated:YES completion:nil];
  230. return;
  231. }
  232. // Login Flow
  233. if (_user.hidden && _password.hidden && versionMajor >= k_flow_version_available) {
  234. appDelegate.activeLoginWeb = [CCLoginWeb new];
  235. appDelegate.activeLoginWeb.loginType = _loginType;
  236. appDelegate.activeLoginWeb.delegate = self;
  237. appDelegate.activeLoginWeb.urlBase = self.baseUrl.text;
  238. [appDelegate.activeLoginWeb presentModalWithDefaultTheme:self];
  239. }
  240. // NO Login Flow available
  241. if (versionMajor < k_flow_version_available) {
  242. [self.loginTypeView setHidden:YES];
  243. _imageUser.hidden = NO;
  244. _user.hidden = NO;
  245. _imagePassword.hidden = NO;
  246. _password.hidden = NO;
  247. [_user becomeFirstResponder];
  248. }
  249. }
  250. });
  251. }];
  252. [task resume];
  253. }
  254. - (void)trustedCerticateAccepted
  255. {
  256. NSLog(@"[LOG] Certificate trusted");
  257. }
  258. - (void)trustedCerticateDenied
  259. {
  260. if (_loginType == loginModifyPasswordUser)
  261. [self handleAnnulla:self];
  262. }
  263. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  264. {
  265. // The pinnning check
  266. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  267. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  268. } else {
  269. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  270. }
  271. }
  272. - (BOOL)serverStatus:(NSData *)data
  273. {
  274. NSError *error;
  275. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  276. if (error) {
  277. return false;
  278. }
  279. serverProductName = [jsongParsed valueForKey:@"productname"];
  280. serverVersion = [jsongParsed valueForKey:@"version"];
  281. serverVersionString = [jsongParsed valueForKey:@"versionstring"];
  282. NSArray *arrayVersion = [serverVersionString componentsSeparatedByString:@"."];
  283. if (arrayVersion.count >= 3) {
  284. versionMajor = [arrayVersion[0] integerValue];
  285. versionMicro = [arrayVersion[1] integerValue];
  286. versionMinor = [arrayVersion[2] integerValue];
  287. } else {
  288. return false;
  289. }
  290. return true;
  291. }
  292. #pragma --------------------------------------------------------------------------------------------
  293. #pragma mark == Login ==
  294. #pragma --------------------------------------------------------------------------------------------
  295. - (void)loginCloudUrl:(NSString *)url user:(NSString *)user password:(NSString *)password
  296. {
  297. NSError *error = [[NCNetworkingSync sharedManager] checkServer:[NSString stringWithFormat:@"%@%@", url, webDAV] user:user userID:user password:password];
  298. dispatch_async(dispatch_get_main_queue(), ^{
  299. self.login.enabled = NO;
  300. self.loadingBaseUrl.hidden = NO;
  301. if (!error) {
  302. // account
  303. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  304. if (_loginType == loginModifyPasswordUser) {
  305. // Change Password
  306. tableAccount *tbAccount = [[NCManageDatabase sharedInstance] setAccountPassword:account password:password];
  307. // Setting appDelegate active account
  308. [appDelegate settingActiveAccount:tbAccount.account activeUrl:tbAccount.url activeUser:tbAccount.user activeUserID:tbAccount.userID activePassword:tbAccount.password];
  309. [self.delegate loginSuccess:_loginType];
  310. [self dismissViewControllerAnimated:YES completion:nil];
  311. } else {
  312. [[NCManageDatabase sharedInstance] deleteAccount:account];
  313. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:password loginFlow:false];
  314. // Read User Profile
  315. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:account];
  316. metadataNet.action = actionGetUserProfile;
  317. [appDelegate.netQueue addOperation:[[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:user withUserID:user withPassword:password withUrl:url]];
  318. }
  319. } else {
  320. if ([error code] != NSURLErrorServerCertificateUntrusted) {
  321. NSString *description = [error.userInfo objectForKey:@"NSLocalizedDescription"];
  322. NSString *message = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedStringFromTable(@"_not_possible_connect_to_server_", @"Error", nil), description];
  323. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  324. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  325. [alertController addAction:okAction];
  326. [self presentViewController:alertController animated:YES completion:nil];
  327. }
  328. }
  329. self.login.enabled = YES;
  330. self.loadingBaseUrl.hidden = YES;
  331. });
  332. }
  333. #pragma --------------------------------------------------------------------------------------------
  334. #pragma mark ==== User Profile ====
  335. #pragma --------------------------------------------------------------------------------------------
  336. - (void)getUserProfileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  337. {
  338. [[NCManageDatabase sharedInstance] deleteAccount:metadataNet.account];
  339. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  340. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  341. [alertController addAction:okAction];
  342. [self presentViewController:alertController animated:YES completion:nil];
  343. }
  344. - (void)getUserProfileSuccess:(CCMetadataNet *)metadataNet userProfile:(OCUserProfile *)userProfile
  345. {
  346. // Verify if the account already exists
  347. if (userProfile.id.length > 0 && self.baseUrl.text.length > 0 && self.user.text.length > 0) {
  348. tableAccount *accountAlreadyExists = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"url = %@ AND user = %@ AND userID != %@", self.baseUrl.text, userProfile.id, self.user.text]];
  349. if (accountAlreadyExists) {
  350. [[NCManageDatabase sharedInstance] deleteAccount:metadataNet.account];
  351. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:[NSString stringWithFormat:NSLocalizedString(@"_account_already_exists_", nil), userProfile.id] preferredStyle:UIAlertControllerStyleAlert];
  352. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  353. [alertController addAction:okAction];
  354. [self presentViewController:alertController animated:YES completion:nil];
  355. return;
  356. }
  357. }
  358. // Verify if account is a valid account
  359. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account = %@", metadataNet.account]];
  360. if (account) {
  361. // Update User (+ userProfile.id)
  362. (void)[[NCManageDatabase sharedInstance] setAccountUserProfile:userProfile];
  363. // Set this account as default
  364. tableAccount *account = [[NCManageDatabase sharedInstance] setAccountActive:metadataNet.account];
  365. if (account) {
  366. // Setting appDelegate active account
  367. [appDelegate settingActiveAccount:account.account activeUrl:account.url activeUser:account.user activeUserID:account.userID activePassword:account.password];
  368. [self.delegate loginSuccess:_loginType];
  369. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  370. [self dismissViewControllerAnimated:YES completion:nil];
  371. });
  372. }
  373. }
  374. }
  375. #pragma --------------------------------------------------------------------------------------------
  376. #pragma mark == TextField ==
  377. #pragma --------------------------------------------------------------------------------------------
  378. -(void)textFieldDidBeginEditing:(UITextField *)textField
  379. {
  380. if (textField == self.password) {
  381. self.toggleVisiblePassword.hidden = NO;
  382. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  383. }
  384. }
  385. -(void)textFieldDidEndEditing:(UITextField *)textField
  386. {
  387. if (textField == self.password) {
  388. self.toggleVisiblePassword.hidden = YES;
  389. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  390. }
  391. }
  392. #pragma --------------------------------------------------------------------------------------------
  393. #pragma mark === CCLoginDelegateWeb ===
  394. #pragma --------------------------------------------------------------------------------------------
  395. - (void)loginSuccess:(NSInteger)loginType
  396. {
  397. [self.delegate loginSuccess:_loginType];
  398. }
  399. - (void)loginWebClose
  400. {
  401. appDelegate.activeLoginWeb = nil;
  402. [self dismissViewControllerAnimated:YES completion:nil];
  403. }
  404. #pragma --------------------------------------------------------------------------------------------
  405. #pragma mark == Action ==
  406. #pragma --------------------------------------------------------------------------------------------
  407. - (void)tabBottomLabel
  408. {
  409. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NCBrandOptions sharedInstance].linkLoginProvider]];
  410. }
  411. - (IBAction)handlebaseUrlchange:(id)sender
  412. {
  413. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  414. [self performSelector:@selector(testUrl) withObject:nil];
  415. }
  416. - (IBAction)handleButtonLogin:(id)sender
  417. {
  418. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  419. [self testUrl];
  420. return;
  421. }
  422. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  423. // remove last char if /
  424. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  425. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  426. NSString *url = self.baseUrl.text;
  427. NSString *user = self.user.text;
  428. NSString *password = self.password.text;
  429. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  430. [self loginCloudUrl:url user:user password:password];
  431. });
  432. }
  433. }
  434. - (IBAction)handleAnnulla:(id)sender
  435. {
  436. [self dismissViewControllerAnimated:YES completion:nil];
  437. }
  438. - (IBAction)handleToggleVisiblePassword:(id)sender
  439. {
  440. NSString *currentPassword = self.password.text;
  441. self.password.secureTextEntry = ! self.password.secureTextEntry;
  442. self.password.text = @"";
  443. self.password.text = currentPassword;
  444. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [NCBrandColor sharedInstance].customerText};
  445. }
  446. - (IBAction)handleLoginTypeView:(id)sender
  447. {
  448. if (_user.hidden && _password.hidden) {
  449. _imageUser.hidden = NO;
  450. _user.hidden = NO;
  451. _imagePassword.hidden = NO;
  452. _password.hidden = NO;
  453. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  454. } else {
  455. _imageUser.hidden = YES;
  456. _user.hidden = YES;
  457. _imagePassword.hidden = YES;
  458. _password.hidden = YES;
  459. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  460. }
  461. }
  462. @end