diff --git a/app/Global.scala b/app/Global.scala index 3b9253c..cf6dc3b 100644 --- a/app/Global.scala +++ b/app/Global.scala @@ -10,34 +10,37 @@ import anorm._ package object globals { - def settings(country: Option[String], key: String, data_type: Integer = 2): Any = { - Logger.warn("Your message key:%s option:%s type: %s".format(key, country.getOrElse("NONE!"), data_type)) + def settings(country: String, key: String, data_type: Integer = 2): Any = { if (data_type == 0) { // Boolean - val return_variable0 = Play.current.configuration.getBoolean(country.get.concat(".").concat(key)).getOrElse(false) - if ((return_variable0).getClass == (false).getClass && !(return_variable0 == false && Play.current.configuration.getBoolean(country.get.concat(".").concat(key)).getOrElse(true))) + val return_variable0 = Play.current.configuration.getBoolean(country.concat(".").concat(key)).getOrElse(false) + if ((return_variable0).getClass == (false).getClass && !(return_variable0 == false && Play.current.configuration.getBoolean(country.concat(".").concat(key)).getOrElse(true))) return return_variable0 - else - return "error" + else { + Logger.warn("Error on country-setting key. Country: %s, Key: %s, Type: boolean".format(country, key)) + return false + } } if (data_type == 1) { // Double - val return_variable1 = Play.current.configuration.getDouble(country.get.concat(".").concat(key)).getOrElse(-1.01) + val return_variable1 = Play.current.configuration.getDouble(country.concat(".").concat(key)).getOrElse(-1.01) if ((return_variable1).getClass == (0.1).getClass && return_variable1 != -1.01) return return_variable1 - else - return "error" + else { + Logger.warn("Error on country-setting key. Country: %s, Key: %s, Type: double".format(country, key)) + return -1.01 + } } if (data_type == 2) { // String - val return_variable2 = Play.current.configuration.getString(country.get.concat(".").concat(key)).getOrElse("Not Set") + val return_variable2 = Play.current.configuration.getString(country.concat(".").concat(key)).getOrElse("Not Set") if ((return_variable2).getClass == ("oi").getClass && return_variable2 != "Not Set") return return_variable2 - else - return "error" + else { + Logger.warn("Error on country-setting key. Country: %s, Key: %s, Type: string".format(country, key)) + return "Not Set" + } } - if (key == "country_fee_deposit_percent") - return key.concat(" ") return key.concat(" ") } @@ -45,191 +48,276 @@ package object globals { val masterDBWallet = "wallet" val masterDBTrusted = "trust" - try { - val br_initial_capital_d = settings(Option("br"), "country_system_initial_crypto_capital", 1).asInstanceOf[Double] - val br_local_administrator_s = settings(Option("br"), "country_local_administrator", 2).asInstanceOf[String] - val br_global_administrator_s = settings(Option("br"), "country_global_administrator", 2).asInstanceOf[String] - val br_partner1_account_s = settings(Option("br"), "country_partner1_account").asInstanceOf[String] - val br_partner2_account_s = settings(Option("br"), "country_partner2_account").asInstanceOf[String] - val br_partner1_name_s = settings(Option("br"), "country_partner1_name").asInstanceOf[String] - val br_partner1_url_s = settings(Option("br"), "country_partner1_url").asInstanceOf[String] - val br_partner1_info_s = settings(Option("br"), "country_partner1_info").asInstanceOf[String] - val br_partner2_name_s = settings(Option("br"), "country_partner2_name").asInstanceOf[String] - val br_partner2_url_s = settings(Option("br"), "country_partner2_url").asInstanceOf[String] - val br_partner2_info_s = settings(Option("br"), "country_partner2_info").asInstanceOf[String] - - if (Play.current.configuration.getBoolean("meta.devdb").getOrElse(false)) { - DB.withConnection(globals.masterDB)({ implicit c => - SQL""" - begin; - delete from users_name_info; - delete from users_connections; - delete from users_passwords; - delete from users_tfa_secrets; - delete from users_backup_otps; - delete from totp_tokens_blacklist; - delete from event_log; - delete from tokens; - delete from trusted_action_requests; - delete from balances; - delete from orders; - delete from currencies; - delete from users; - delete from image; - - insert into image (image_id, name) values (0, 'null'); - - select currency_insert(1, 'BRL', 'br', 0, 0); - select currency_insert(2, 'USD', 'us', 0, 0); - - insert into users(id, email) values (0, ''); - insert into balances (user_id, currency) select 0, currency from currencies; - update balances set balance = 0, balance_c = ${br_initial_capital_d} where currency = 'BRL' and user_id = 0; - update balances set balance = 0, balance_c = 70000 where currency = 'USD' and user_id = 0; - - select create_user(${br_local_administrator_s}, 'Fada00Fada', true, null, 'en', 'br', true, ''); - select create_user(${br_global_administrator_s}, 'aaa222', true, null, 'en', 'ru', true, ''); - - select create_user(${br_partner1_account_s}, 'aaa222', true, null, 'en', 'fr', true, ''); - select create_user(${br_partner2_account_s}, 'aaa222', true, null, 'en', 'fr', true, ''); - - select insert_as_admin('us', ${br_global_administrator_s}, 'admin_g1'); - select insert_as_admin('us', ${br_local_administrator_s}, 'admin_l1'); - select insert_as_admin('br', ${br_global_administrator_s}, 'admin_g1'); - select insert_as_admin('br', ${br_local_administrator_s}, 'admin_l1'); - select insert_as_admin('br', ${br_local_administrator_s}, 'admin_o1'); - - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Marcelo', 'Simão', 'Boczko', '999.090.089-98', 'doc_pdf.pdf', 'doc_pdf.pdf', '(12)99324-0988', 'doc5', true, true, true, true, true from users where email=${br_local_administrator_s}; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Yura', '', 'Mitrofanov', '097.455.645-09', '140.png', 'doc_38.jpg', '(53)30823-098', 'doc5', false, false, false, true, false from users where email=${br_global_administrator_s}; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, ${br_partner1_name_s}, ${br_partner1_url_s}, ${br_partner1_info_s}, '', '', '', '', '', false, false, false, false, false from users where email=${br_partner1_account_s}; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, ${br_partner2_name_s}, ${br_partner2_url_s}, ${br_partner2_info_s}, '', '', '', '', '', false, false, false, false, false from users where email=${br_partner2_account_s}; - - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${br_local_administrator_s}), '745', 'Agency B', 'Account B', 'Crypto-Trade.net', 'partner_account@gmail.com'; - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${br_global_administrator_s}), '341', '8788-X', '677.789-9', 'Crypto-Trade.net', ''; - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${br_partner1_account_s}), '', '', '', ${br_partner1_url_s}, ${br_partner1_account_s}; - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${br_partner2_account_s}), '', '', '', ${br_partner2_url_s}, ${br_partner2_account_s}; - - update balances set balance = 1000 where currency = 'BRL' and user_id = (select id from users where email=${br_local_administrator_s}); - update balances set balance = 1000 where currency = 'BRL' and user_id = (select id from users where email=${br_global_administrator_s}); - - select create_user('a', 'a', true, null, 'en', 'us', false, ''); - select create_user('test@hotmail.ru', 'pass01', true, null, 'ru', 'br', false, ''); - select create_user('test@gmail.com', 'pass02', true, null, 'en', 'br', false, ''); - select create_user('test@yahoo.com.br', 'pass03', true, null, 'br', 'br', false, ''); - select create_user('testru@gmail.ru', 'pass04', true, null, 'ru', 'br', false, ''); - - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Test', 'Test-middle_name', 'Tes-last_name', '', '', '', '', 'doc5', false, false, false, false, false from users where email='test@hotmail.ru'; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Test', '', 'last name', '566.432.789-03', 'doc39.jpg', '140.png', '(11)32580-342', 'doc5', false, false, false, true, false from users where email='test@gmail.com'; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'TestBR', '', 'sobrenome', '', 'doc39.jpg', 'doc_37.JPG', '(15)99707-0000', '', false, false, false, true, false from users where email='test@yahoo.com.br'; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'TestRU', '', 'skovsk', '343.782.121-34', 'doc_38.jpg', '', '(11)95454-0993', 'doc5', true, true, true, true, false from users where email='testru@gmail.ru'; - insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Aaaaa', 'midA', 'LastA', '333.988.454-08', 'doc_PDF.pdf', 'doc_37.jpg', '(19)23240-434', 'doc5', true, true, true, true, false from users where email='a'; - - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='a'), '237', 'Agency A', 'Account A', 'Crypto-Trade.net', 'qwqwqw@ioe.cs'; - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='test@yahoo.com.br'), '237', '65665', '00685343-0', '', ''; - insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='testru@gmail.ru'), '341', '352323-c', '67345-9', '', ''; + //Initiating Database + initiateDatabase() + var initiate_country_as_number = 1 + initiateCountry("br") // Brazil + // @@@ only first ountry works for now... When unique user defined with country then other countries would work + //initiateCountry("dc") // Default Country + temporaryInitiateCountry("dc") + temporaryPopulateDatabase() - commit; - """.execute() - }) - } - } catch { + val userModel = new UserModel(masterDB) + val logModel = new LogModel(masterDB) + val engineModel = new EngineModel(masterDB) + val userTrustModel = new UserTrustModel(masterDBTrusted) - // XXX: any kind of error in the SQL above will cause this cryptic exception: - // org.postgresql.util.PSQLException: Cannot change transaction read-only property in the middle of a transaction. - case error: Throwable => Logger.error(error.toString) + // create UserTrust actor + val userTrustActor = current.configuration.getBoolean("usertrustservice.enabled").getOrElse(false) match { + case true => Some(Akka.system.actorOf(UserTrustService.props(userTrustModel))) + case false => None } - /* - --select create_user($country_local_administrator, 'Fada00Fada', true, null, 'en', 'br', true, ''); - --insert into balances (user_id, currency) select (select id from users where email=$country_local_administrator), currency from currencies;; - --insert into users_passwords (user_id, password) values ((select id from users where email=$country_local_administrator), crypt('qwe', gen_salt('bf', 8)));; - - --select create_user($country_global_administrator, 'qwerty123', true, null, 'en', 'ru', false, ''); - --insert into balances (user_id, currency) select (select id from users where email=$country_global_administrator), currency from currencies;; - --insert into users_passwords (user_id, password) values ((select id from users where email=$country_global_administrator), crypt('qwe', gen_salt('bf', 8)));; - - + def initiateDatabase(): Boolean = { + try { + if (Play.current.configuration.getBoolean("meta.devdb").getOrElse(false)) { + DB.withConnection(globals.masterDB)({ implicit c => + SQL""" + begin; + delete from users_name_info; + delete from users_connections; + delete from users_passwords; + delete from users_tfa_secrets; + delete from users_backup_otps; + delete from totp_tokens_blacklist; + delete from event_log; + delete from tokens; + delete from trusted_action_requests; + delete from balances; + delete from orders; + delete from currencies; + delete from users; + delete from image; + + insert into image (image_id, name) values (0, 'null'); + insert into users(id, email, user_country) values (0, 'system_account', 'dc'); + commit; + """.execute() + }) + } + return true + } catch { + case error: Throwable => + Logger.error(error.toString) + return false + } + } - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='mboczko@yahoo.com'), 'br', 'RFW', 'Op', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 4566.9808, 15.76, '', '', '001 - Banco do Brasil', '78887-x', '213.423.2-9', '2016-12-22 01:18:59.842', 120, 4420.8, 'comment', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='a2terminator@mail.ru'), 'br', 'D', 'Op', '', '2016-12-22 01:18:59.842', 'BRL', 74.98, 0, 'recibo1.jpg', '', '237', '5454-0', '4645-8', '2016-12-22 01:18:59.842', 0, 0, '', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='mboczko@yahoo.com'), 'us', 'W', 'Rj', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'USD', 320, 0.55, '', '', 'City-090', 'bvbvb', 'bvbvb', '2016-12-22 11:18:59.842', 12121, 0, 'bank info not correct', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='mboczko@yahoo.com'), 'br', 'DCS', 'OK', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 620, 1.55, 'recibo2.jpg', '', 'City-090', '8787', '455454-0', '2016-12-12 01:18:59.842', 121212, 618.45, 'bank OK, receipt OK', 'key1 OK from CT', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='testru@gmail.ru'), 'br', 'S', 'Op', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 20.03, 0.05, '', '', '', '', '', '2016-12-22 01:47:00.842', 0, 0, '', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='testru@gmail.ru'), 'br', 'DCS', 'OK', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 980, 0, 'recibo1.jpg', '', '001', '8787', '455454-0', '2016-10-17 01:18:59.842', 121212, 980, 'bank OK, receipt OK', 'key1 OK from CT', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='mboczko@yahoo.com'), 'us', 'D', 'OK', '', '2016-12-22 01:18:59.842', 'USD', 7654.90, 43.15, 'recibo4.gif', '', 'BofA', '8987-tr', '343434-098', '2016-11-03 01:18:59.842', 121212, 7611.75, 'bank OK, receipt OK', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='testru@gmail.ru'), 'us', 'V', 'Op', '', '2016-12-22 01:18:59.842', 'USD', 0, 0, 'doc_38.jpg', '', '', '', '', '2016-12-22 01:18:59.842', 12121, 0, '', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='test@yahoo.com.br'), 'br', 'W.', 'Lk', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 37870.98, 5.55, 'recibo5.jpg', '', '001', '8787', '455454-0', '2016-12-22 01:18:59.842', 121212, 37865.43, 'bank OK, receipt OK', 'key1 OK from CT', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='test@yahoo.com.br'), 'br', 'D', 'Ch', 'Crypto-Trade.net', '2016-12-22 01:18:59.842', 'BRL', 78.00, 0, 'recibo6.png', '', '341', '7876', '7897', '2016-12-22 01:18:59.842', 121212, 780, 'value declared wrong. confirmed at bank 780', '', '', 0; - insert into orders (user_id, country_id, order_type, status, partner, created, currency, initial_value, total_fee, doc1, doc2, bank, agency, account, closed, processed_by, net_value, comment, key1, key2, image_id) select (select id from users where email='mboczko@yahoo.com'), 'us', 'W.', 'Rj','' , '2016-12-22 01:18:59.842', 'USD', 320, 0.55, '','' , 'City-090', 'bvbvb', 'bvbvb', '2016-12-22 01:28:07.842', 12121, 0, 'bank info not correct', '', '', 0; + def initiateCountry(current_country: String): Boolean = { + val country_code_s = settings(current_country, "country_code", 2).asInstanceOf[String] + val currency_code_s = settings(current_country, "country_currency_code", 2).asInstanceOf[String] + val initial_capital_d = settings(current_country, "country_system_initial_crypto_capital", 1).asInstanceOf[Double] + val local_admin1_s = settings(current_country, "country_local_admin1", 2).asInstanceOf[String] + val local_admin1_country_s = settings(current_country, "country_local_admin1_country", 2).asInstanceOf[String] + val local_admin2_s = settings(current_country, "country_local_admin2", 2).asInstanceOf[String] + val local_admin2_country_s = settings(current_country, "country_local_admin2_country", 2).asInstanceOf[String] + val global_admin1_s = settings(current_country, "country_global_admin1", 2).asInstanceOf[String] + val global_admin1_country_s = settings(current_country, "country_global_admin1_country", 2).asInstanceOf[String] + val global_admin2_s = settings(current_country, "country_global_admin2", 2).asInstanceOf[String] + val global_admin2_country_s = settings(current_country, "country_global_admin2_country", 2).asInstanceOf[String] + val partner1_account_s = settings(current_country, "country_partner1_account", 2).asInstanceOf[String] + val partner2_account_s = settings(current_country, "country_partner2_account", 2).asInstanceOf[String] + val partner1_name_s = settings(current_country, "country_partner1_name", 2).asInstanceOf[String] + val partner1_url_s = settings(current_country, "country_partner1_url", 2).asInstanceOf[String] + val partner1_info_s = settings(current_country, "country_partner1_info", 2).asInstanceOf[String] + val partner2_name_s = settings(current_country, "country_partner2_name", 2).asInstanceOf[String] + val partner2_url_s = settings(current_country, "country_partner2_url", 2).asInstanceOf[String] + val partner2_info_s = settings(current_country, "country_partner2_info", 2).asInstanceOf[String] + try { + if (Play.current.configuration.getBoolean("meta.devdb").getOrElse(false)) { + DB.withConnection(globals.masterDB)({ implicit c => + SQL""" + begin; + select currency_insert(${initiate_country_as_number}, ${currency_code_s}, ${country_code_s}, 0, 0); + insert into balances (user_id, currency) select 0, ${currency_code_s}; + update balances set balance = 0, balance_c = ${initial_capital_d} where currency = ${currency_code_s} and user_id = 0; + + if not user_exists (${local_admin1_s}, $local_admin1_country_s) then + select create_user(${local_admin1_s}, $local_admin1_country_s, 'asd123', true, null, 'en', true, ''); + select insert_as_admin(${country_code_s}, ${local_admin1_s}, $local_admin1_country_s, 'admin_l1'); + end if; + + if not user_exists (${local_admin2_s}, $local_admin2_country_s) then + select create_user(${local_admin2_s}, $local_admin2_country_s, 'asd123', true, null, 'en', true, ''); + select insert_as_admin(${country_code_s}, ${local_admin2_s}, $local_admin2_country_s, 'admin_l2'); + end if; + + if not user_exists (${global_admin1_s}, $global_admin1_country_s) then + select create_user(${global_admin1_s}, $global_admin1_country_s, 'aaa222', true, null, 'en', true, ''); + select insert_as_admin(${country_code_s}, ${global_admin1_s}, $global_admin1_country_s, 'admin_g1'); + end if; + + if not user_exists (${global_admin2_s}, $global_admin2_country_s) then + select create_user(${global_admin2_s}, $global_admin2_country_s, 'aaa222', true, null, 'en', true, ''); + select insert_as_admin(${country_code_s}, ${global_admin2_s}, $global_admin2_country_s, 'admin_g2'); + end if; + + select create_user(${partner1_account_s}, ${currency_code_s}, 'zxc111', true, null, 'en', true, ''); + select create_user(${partner2_account_s}, ${currency_code_s}, 'zxc111', true, null, 'en', true, ''); + + + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Marcelo', 'Simão', 'Boczko', '999.090.089-98', 'doc_pdf.pdf', 'doc_pdf.pdf', '(12)99324-0988', 'doc5', true, true, true, true, true from users where email=${local_admin1_s}; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Yura', '', 'Mitrofanov', '097.455.645-09', '140.png', 'doc_38.jpg', '(53)30823-098', 'doc5', false, false, false, true, false from users where email=${global_admin1_s}; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, ${partner1_name_s}, ${partner1_url_s}, ${partner1_info_s}, '', '', '', '', '', false, false, false, false, false from users where email=${partner1_account_s}; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, ${partner2_name_s}, ${partner2_url_s}, ${partner2_info_s}, '', '', '', '', '', false, false, false, false, false from users where email=${partner2_account_s}; + + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${local_admin1_s}), '745', 'Agency B', 'Account B', 'Crypto-Trade.net', 'partner_account@gmail.com'; + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${global_admin1_s}), '341', '8788-X', '677.789-9', 'Crypto-Trade.net', ''; + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${partner1_account_s}), '', '', '', ${partner1_url_s}, ${partner1_account_s}; + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email=${partner2_account_s}), '', '', '', ${partner2_url_s}, ${partner2_account_s}; + commit; + """.execute() + }) + } + initiate_country_as_number = initiate_country_as_number + 1 + return true + } catch { + case error: Throwable => + Logger.error(error.toString) + return false + } + } - */ + def temporaryInitiateCountry(current_country: String): Boolean = { + try { + val country_code_s = settings(current_country, "country_code", 2).asInstanceOf[String] + val currency_code_s = settings(current_country, "country_currency_code", 2).asInstanceOf[String] + val initial_capital_d = settings(current_country, "country_system_initial_crypto_capital", 1).asInstanceOf[Double] + val local_admin1_s = settings(current_country, "country_local_admin1", 2).asInstanceOf[String] + val global_admin1_s = settings(current_country, "country_global_admin1", 2).asInstanceOf[String] + val partner1_account_s = settings(current_country, "country_partner1_account", 2).asInstanceOf[String] + val partner2_account_s = settings(current_country, "country_partner2_account", 2).asInstanceOf[String] + val partner1_name_s = settings(current_country, "country_partner1_name", 2).asInstanceOf[String] + val partner1_url_s = settings(current_country, "country_partner1_url", 2).asInstanceOf[String] + val partner1_info_s = settings(current_country, "country_partner1_info", 2).asInstanceOf[String] + val partner2_name_s = settings(current_country, "country_partner2_name", 2).asInstanceOf[String] + val partner2_url_s = settings(current_country, "country_partner2_url", 2).asInstanceOf[String] + val partner2_info_s = settings(current_country, "country_partner2_info", 2).asInstanceOf[String] + if (Play.current.configuration.getBoolean("meta.devdb").getOrElse(false)) { + DB.withConnection(globals.masterDB)({ implicit c => + SQL""" + begin; + select currency_insert(${initiate_country_as_number}, ${currency_code_s}, ${country_code_s}, 0, 0); + insert into balances (user_id, currency) select 0, ${currency_code_s}; + update balances set balance = 0, balance_c = ${initial_capital_d} where currency = ${currency_code_s} and user_id = 0; + + select create_user(${local_admin1_s}, 'br', 'Fada00Fada', true, null, 'en', true, ''); + select create_user(${global_admin1_s}, 'ru', 'aaa222', true, null, 'en', true, ''); + + select create_user(${partner1_account_s}, 'fr', 'aaa222', true, null, 'en', true, ''); + select create_user(${partner2_account_s}, 'fr', 'aaa222', true, null, 'en', true, ''); + select insert_as_admin(${country_code_s}, ${global_admin1_s}, 'admin_g1'); + select insert_as_admin(${country_code_s}, ${local_admin1_s}, 'admin_l1'); - val userModel = new UserModel(masterDB) - val logModel = new LogModel(masterDB) - val engineModel = new EngineModel(masterDB) + commit; + """.execute() + }) + } + initiate_country_as_number = initiate_country_as_number + 1 + return true + } catch { + case error: Throwable => + Logger.error(error.toString) + return false + } + } - val userTrustModel = new UserTrustModel(masterDBTrusted) + def temporaryPopulateDatabase(): Boolean = { + try { + if (Play.current.configuration.getBoolean("meta.devdb").getOrElse(false)) { + DB.withConnection(globals.masterDB)({ implicit c => + SQL""" + begin; + select insert_as_admin('br', 'mboczko@yahoo.com', 'admin_o1'); + update balances set balance = 5000 where currency = 'BRL' and user_id = (select id from users where email='mboczko@yahoo.com'); + + select create_user('a', 'dc', 'a', true, null, 'en', false, ''); + select create_user('test@hotmail.ru', 'br', 'pass01', true, null, 'ru', false, ''); + select create_user('test@gmail.com', 'br', 'pass02', true, null, 'en', false, ''); + select create_user('test@yahoo.com.br', 'br', 'pass03', true, null, 'pt', false, ''); + select create_user('testru@gmail.ru', 'br', 'pass04', true, null, 'ru', false, ''); + + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Test', 'Test-middle_name', 'Tes-last_name', '', '', '', '', 'doc5', false, false, false, false, false from users where email='test@hotmail.ru'; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Test', '', 'last name', '566.432.789-03', 'doc39.jpg', '140.png', '(11)32580-342', 'doc5', false, false, false, true, false from users where email='test@gmail.com'; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'TestBR', '', 'sobrenome', '', 'doc39.jpg', 'doc_37.JPG', '(15)99707-0000', '', false, false, false, true, false from users where email='test@yahoo.com.br'; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'TestRU', '', 'skovsk', '343.782.121-34', 'doc_38.jpg', '', '(11)95454-0993', 'doc5', true, true, true, true, false from users where email='testru@gmail.ru'; + insert into users_name_info (user_id, first_name, middle_name, last_name, doc1, doc2, doc3, doc4, doc5, ver1, ver2, ver3, ver4, ver5) select id, 'Aaaaa', 'midA', 'LastA', '333.988.454-08', 'doc_PDF.pdf', 'doc_37.jpg', '(19)23240-434', 'doc5', true, true, true, true, false from users where email='a'; + + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='a'), '237', 'Agency A', 'Account A', 'Crypto-Trade.net', 'qwqwqw@ioe.cs'; + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='test@yahoo.com.br'), '237', '65665', '00685343-0', '', ''; + insert into users_connections (user_id, bank, agency, account, partner, partner_account) select (select id from users where email='testru@gmail.ru'), '341', '352323-c', '67345-9', '', ''; + + commit; + """.execute() + + }) + } + return true + } catch { + // XXX: any kind of error in the SQL above will cause this cryptic exception: + // org.postgresql.util.PSQLException: Cannot change transaction read-only property in the middle of a transaction. + case error: Throwable => + Logger.error(error.toString) + return false + } + } def numberFormat(value: AnyVal): String = { - if (settings(Option("br"), "country_decimal_separator").asInstanceOf[String] == ',') + if (settings("dc", "country_decimal_separator").asInstanceOf[String] == ',') return value.toString else return (value.toString).replace('.', ',') } def calculate_local_fee(order_type: String, initial_value: BigDecimal = 0): BigDecimal = { - val percentage = (100 - settings(Option("br"), "country_fees_global_percentage", 1).asInstanceOf[Double]) * 0.01 + val default_country = "dc" // ### Must change to user's country + val percentage = (100 - settings(default_country, "country_fees_global_percentage", 1).asInstanceOf[Double]) * 0.01 var low_value_fee = 0.0 - if (initial_value < settings(Option("br"), "country_minimum_value", 1).asInstanceOf[Double]) { - low_value_fee = settings(Option("br"), "country_minimum_value", 1).asInstanceOf[Double] * 0.02 + if (initial_value < settings(default_country, "country_minimum_value", 1).asInstanceOf[Double]) { + low_value_fee = settings(default_country, "country_minimum_value", 1).asInstanceOf[Double] * 0.02 } if (order_type == "D") { - return initial_value * settings(Option("br"), "country_fee_deposit_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee + return initial_value * settings(default_country, "country_fee_deposit_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee } else if (order_type == "S") { - return initial_value * settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "S.") { - return initial_value * settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "DCS") { - return initial_value * (settings(Option("br"), "country_fee_deposit_percent", 1).asInstanceOf[Double] + settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee + return initial_value * (settings(default_country, "country_fee_deposit_percent", 1).asInstanceOf[Double] + settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee } else if (order_type == "W") { // withdrawal to a preferential bank - return settings(Option("br"), "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + initial_value * settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee + return settings(default_country, "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + initial_value * settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee } else if (order_type == "W.") { // withdrawal to a non preferential bank - return settings(Option("br"), "country_nominal_fee_withdrawal_not_preferential_bank", 1).asInstanceOf[Double] + initial_value * settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee + return settings(default_country, "country_nominal_fee_withdrawal_not_preferential_bank", 1).asInstanceOf[Double] + initial_value * settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage + low_value_fee } else if (order_type == "RFW") { // withdrawal to a preferential bank - return settings(Option("br"), "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + initial_value * (settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(Option("br"), "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee + return settings(default_country, "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + initial_value * (settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(default_country, "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee } else if (order_type == "RFW.") { // withdrawal to a non preferential bank - return settings(Option("br"), "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + settings(Option("br"), "country_nominal_fee_withdrawal_not_preferential_bank", 1).asInstanceOf[Double] + initial_value * (settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(Option("br"), "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee + return settings(default_country, "country_nominal_fee_withdrawal_preferential_bank", 1).asInstanceOf[Double] + settings(default_country, "country_nominal_fee_withdrawal_not_preferential_bank", 1).asInstanceOf[Double] + initial_value * (settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(default_country, "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + low_value_fee } else if (order_type == "F") { - return initial_value * settings(Option("br"), "country_fee_tofiat_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_tofiat_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else return 0 } def calculate_global_fee(order_type: String, initial_value: BigDecimal = 0): BigDecimal = { - val percentage = settings(Option("br"), "country_fees_global_percentage", 1).asInstanceOf[Double] * 0.01 + val default_country = "dc" //### Must change to user's country + val percentage = settings(default_country, "country_fees_global_percentage", 1).asInstanceOf[Double] * 0.01 if (order_type == "D") { - return initial_value * settings(Option("br"), "country_fee_deposit_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_deposit_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "S") { - return initial_value * settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "S.") { - return initial_value * settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "DCS") { - return initial_value * (settings(Option("br"), "country_fee_deposit_percent", 1).asInstanceOf[Double] + settings(Option("br"), "country_fee_send_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + return initial_value * (settings(default_country, "country_fee_deposit_percent", 1).asInstanceOf[Double] + settings(default_country, "country_fee_send_percent", 1).asInstanceOf[Double]) * 0.01 * percentage } else if (order_type == "W" || order_type == "W.") { - return initial_value * settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else if (order_type == "RFW" || order_type == "RFW.") { - return initial_value * (settings(Option("br"), "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(Option("br"), "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage + return initial_value * (settings(default_country, "country_fee_withdrawal_percent", 1).asInstanceOf[Double] + settings(default_country, "country_fee_tofiat_percent", 1).asInstanceOf[Double]) * 0.01 * percentage } else if (order_type == "F") { - return initial_value * settings(Option("br"), "country_fee_tofiat_percent", 1).asInstanceOf[Double] * 0.01 * percentage + return initial_value * settings(default_country, "country_fee_tofiat_percent", 1).asInstanceOf[Double] * 0.01 * percentage } else return 0 } - // create UserTrust actor - val userTrustActor = current.configuration.getBoolean("usertrustservice.enabled").getOrElse(false) match { - case true => Some(Akka.system.actorOf(UserTrustService.props(userTrustModel))) - case false => None - } - } object Global extends GlobalSettings { diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 68f173d..3cbe065 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -119,7 +119,7 @@ class Application @Inject() (jsMessagesFactory: JsMessagesFactory, val messagesA var local_fee = 0.1 var global_fee = 0.1 val decimal_separator = globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_decimal_separator", 2).asInstanceOf[String] - val country_id = securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br") + val country_id = securesocial.core.SecureSocial.currentUser.get.user_country request.body.files map { file => val fileName = file.filename @@ -145,7 +145,7 @@ class Application @Inject() (jsMessagesFactory: JsMessagesFactory, val messagesA } local_fee = globals.calculate_local_fee(order_type, initial_value).toDouble global_fee = globals.calculate_global_fee(order_type, initial_value).toDouble - val success = globals.userModel.create_order_with_picture(request.user.id, country_id, order_type, "Op", partner, globals.settings(Option(securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br")), "country_currency_code", 2).asInstanceOf[String], initial_value, local_fee, global_fee, "", "", partner_account, fileName, image_id) + val success = globals.userModel.create_order_with_picture(request.user.id, country_id, order_type, "Op", partner, globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_currency_code", 2).asInstanceOf[String], initial_value, local_fee, global_fee, "", "", partner_account, fileName, image_id) } } Ok(views.html.exchange.dashboard(request.user)) @@ -169,8 +169,6 @@ class Application @Inject() (jsMessagesFactory: JsMessagesFactory, val messagesA val position3 = file.key.substring(position2 + 1, file.key.length).indexOf("|") + position2 + 1 val position4 = file.key.substring(position3 + 1, file.key.length).indexOf("|") + position3 + 1 val decimal_separator = globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_decimal_separator", 2).asInstanceOf[String] - //val country_id = securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br") - // = try { Some(s.toDouble) } catch { case _ => None } processed_value = try { ((file.key.substring(0, position)).replace(decimal_separator, ".")).toDouble } catch { @@ -205,7 +203,7 @@ class Application @Inject() (jsMessagesFactory: JsMessagesFactory, val messagesA val docNumber = file.key val user_id = request.user.id val image_id = controllers.Image.saveImage(file.ref.file.getAbsolutePath, fileName, user_id) - var success = globals.userModel.create_order_with_picture(user_id, securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br"), "V", "Op", docNumber, globals.settings(Option(securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br")), "country_currency_code", 2).asInstanceOf[String], 0, 0, 0, "", "", "", fileName, image_id) + var success = globals.userModel.create_order_with_picture(user_id, securesocial.core.SecureSocial.currentUser.get.user_country, "V", "Op", docNumber, globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_currency_code", 2).asInstanceOf[String], 0, 0, 0, "", "", "", fileName, image_id) success = globals.userModel.update_user_doc(user_id, docNumber, image_id, fileName) } Ok(views.html.exchange.dashboard(request.user)) diff --git a/app/controllers/IAPI/APIv1.scala b/app/controllers/IAPI/APIv1.scala index 22a967b..ffc9939 100644 --- a/app/controllers/IAPI/APIv1.scala +++ b/app/controllers/IAPI/APIv1.scala @@ -83,7 +83,7 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec } def orders_list = SecuredAction(ajaxCall = true)(parse.json) { implicit request => - val country = securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br") + val country = securesocial.core.SecureSocial.currentUser.get.user_country val search_criteria = (request.request.body \ "search_criteria").asOpt[String] val search_value = (request.request.body \ "search_value").asOpt[String] val orders_list_info = globals.engineModel.OrderList(Some(request.user.id), country, search_criteria, search_value) @@ -120,7 +120,7 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec } def users_list = SecuredAction(ajaxCall = true)(parse.anyContent) { implicit request => - val country = securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br") + val country = securesocial.core.SecureSocial.currentUser.get.user_country val users_list_info = globals.engineModel.UsersList(country) Ok(Json.toJson(users_list_info.map({ c => Json.obj( @@ -197,6 +197,7 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec Json.obj( "id" -> c.id, "email" -> c.email.getOrElse("").toString, + "user_country" -> c.user_country.getOrElse("").toString, "ip" -> c.ip.getOrElse("").toString, "created" -> c.created.getOrElse(new DateTime(0).toString).toString, "type" -> c.typ.toString @@ -220,7 +221,7 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec } def get_admins = SecuredAction(ajaxCall = true)(parse.anyContent) { implicit request => - val admins = globals.engineModel.GetAdmins(securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br")) + val admins = globals.engineModel.GetAdmins(securesocial.core.SecureSocial.currentUser.get.user_country) Ok(Json.toJson(admins.map({ c => Json.obj( "admin_g1" -> c._1, @@ -234,7 +235,13 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec "email_l1" -> c._9, "email_l2" -> c._10, "email_o1" -> c._11, - "email_o2" -> c._12 + "email_o2" -> c._12, + "user_country_g1" -> c._13, + "user_country_g2" -> c._14, + "user_country_l1" -> c._15, + "user_country_l2" -> c._16, + "user_country_o1" -> c._17, + "user_country_o2" -> c._18 ) }) )) @@ -342,7 +349,7 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec val agency = (request.request.body \ "agency").asOpt[String] val account = (request.request.body \ "account").asOpt[String] val doc1 = (request.request.body \ "doc1").asOpt[String] - if (globals.userModel.create_order(request.user.id, securesocial.core.SecureSocial.currentUser.get.user_country.getOrElse("br"), order_type, status, partner, globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_currency_code", 2).asInstanceOf[String], initial_value, Option(local_fee), Option(global_fee), bank, agency, account, doc1)) { + if (globals.userModel.create_order(request.user.id, securesocial.core.SecureSocial.currentUser.get.user_country, order_type, status, partner, globals.settings(securesocial.core.SecureSocial.currentUser.get.user_country, "country_currency_code", 2).asInstanceOf[String], initial_value, Option(local_fee), Option(global_fee), bank, agency, account, doc1)) { Ok(Json.obj()) } else { BadRequest(Json.obj("message" -> Messages("messages.api.error.failedtocreateorder"))) @@ -409,13 +416,19 @@ class APIv1 @Inject() (val messagesApi: MessagesApi) extends Controller with sec def save_admins = SecuredAction(ajaxCall = true)(parse.json) { implicit request => val country = (request.request.body \ "country").asOpt[String] - val admin_g1 = (request.request.body \ "admin_g1").asOpt[String] - val admin_g2 = (request.request.body \ "admin_g2").asOpt[String] - val admin_l1 = (request.request.body \ "admin_l1").asOpt[String] - val admin_l2 = (request.request.body \ "admin_l2").asOpt[String] - val admin_o1 = (request.request.body \ "admin_o1").asOpt[String] - val admin_o2 = (request.request.body \ "admin_o2").asOpt[String] - if (globals.userModel.save_admins(country, admin_g1, admin_g2, admin_l1, admin_l2, admin_o1, admin_o2)) { + val email_g1 = (request.request.body \ "email_g1").asOpt[String] + val email_g2 = (request.request.body \ "email_g2").asOpt[String] + val email_l1 = (request.request.body \ "email_l1").asOpt[String] + val email_l2 = (request.request.body \ "email_l2").asOpt[String] + val email_o1 = (request.request.body \ "email_o1").asOpt[String] + val email_o2 = (request.request.body \ "email_o2").asOpt[String] + val user_country_g1 = (request.request.body \ "user_country_g1").asOpt[String] + val user_country_g2 = (request.request.body \ "user_country_g2").asOpt[String] + val user_country_l1 = (request.request.body \ "user_country_l1").asOpt[String] + val user_country_l2 = (request.request.body \ "user_country_l2").asOpt[String] + val user_country_o1 = (request.request.body \ "user_country_o1").asOpt[String] + val user_country_o2 = (request.request.body \ "user_country_o2").asOpt[String] + if (globals.userModel.save_admins(country, email_g1, email_g2, email_l1, email_l2, email_o1, email_o2, user_country_g1, user_country_g2, user_country_l1, user_country_l2, user_country_o1, user_country_o2)) { Ok(Json.obj()) } else { BadRequest(Json.obj("message" -> Messages("messages.api.error.failedtosaveadministrators"))) diff --git a/app/controllers/LoginPage.scala b/app/controllers/LoginPage.scala index c9156c7..c73b468 100644 --- a/app/controllers/LoginPage.scala +++ b/app/controllers/LoginPage.scala @@ -76,7 +76,7 @@ class LoginPage @Inject() (val messagesApi: MessagesApi) extends Controller with user <- txbitsUserService.find(authenticator.uid.get) ) yield { Authenticator.delete(authenticator.id) - globals.logModel.logEvent(LogEvent.fromRequest(Some(user.id), Some(user.email), request, LogType.Logout)) + globals.logModel.logEvent(LogEvent.fromRequest(Some(user.id), Some(user.email), Some(user.user_country), request, LogType.Logout)) user } val result = Redirect(to).discardingCookies(Authenticator.discardingCookie) diff --git a/app/controllers/PasswordChange.scala b/app/controllers/PasswordChange.scala index bdfab5b..4eada25 100644 --- a/app/controllers/PasswordChange.scala +++ b/app/controllers/PasswordChange.scala @@ -85,7 +85,7 @@ class PasswordChange @Inject() (val messagesApi: MessagesApi) extends Controller import scala.language.reflectiveCalls // This never actually fails because we already checked that the password is valid in the validators if (globals.userModel.userChangePass(request.user.id, info.currentPassword, info.password)) { - Mailer.sendPasswordChangedNotice(request.user.email, globals.userModel.userPgpByEmail(request.user.email)) + Mailer.sendPasswordChangedNotice(request.user.email, globals.userModel.userPgpByEmail(request.user.email, request.user.user_country)) Redirect(onHandlePasswordChangeGoTo).flashing(Success -> Messages(OkMessage)) } else { BadRequest(views.html.auth.passwordChange(form.withError("currentPassword", Messages(InvalidPasswordMessage)))) diff --git a/app/controllers/ProviderController.scala b/app/controllers/ProviderController.scala index 72d7d66..e37c06c 100644 --- a/app/controllers/ProviderController.scala +++ b/app/controllers/ProviderController.scala @@ -59,8 +59,8 @@ class ProviderController @Inject() (val messagesApi: MessagesApi) extends Contro tfaToken => { val authenticator = SecureSocial.authenticatorFromRequest(request) if (authenticator.isDefined) { - if (globals.userModel.userHasTotp(authenticator.get.email)) { - val user = globals.userModel.totpLoginStep2(authenticator.get.email, authenticator.get.totpSecret.get, tfaToken, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) + if (globals.userModel.userHasTotp(authenticator.get.email, authenticator.get.user_country)) { + val user = globals.userModel.totpLoginStep2(authenticator.get.email, authenticator.get.user_country, authenticator.get.totpSecret.get, tfaToken, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) if (user.isDefined) { Authenticator.save(authenticator.get.complete2fa(user.get.id)) Redirect(toUrl(request2session)).withSession(request2session - SecureSocial.OriginalUrlKey) @@ -90,22 +90,23 @@ class ProviderController @Inject() (val messagesApi: MessagesApi) extends Contro errors => badRequest(errors, request), credentials => { val email = credentials._1.trim + val user_country = credentials._3.trim var user: Option[SocialUser] = None var totp_hash: Option[String] = None // check for 2FA - if (globals.userModel.userHasTotp(email)) { - totp_hash = globals.userModel.totpLoginStep1(email, credentials._2, credentials._3, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) + if (globals.userModel.userHasTotp(email, user_country)) { + totp_hash = globals.userModel.totpLoginStep1(email, user_country, credentials._2, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) } else { - user = globals.userModel.findUserByEmailAndPassword(email, credentials._2, credentials._3, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) + user = globals.userModel.findUserByEmailAndPassword(email, user_country, credentials._2, models.LogModel.headersFromRequest(request), models.LogModel.ipFromRequest(request)) } if (totp_hash.isDefined) { // create session - val authenticator = Authenticator.create(None, totp_hash, email) + val authenticator = Authenticator.create(None, totp_hash, email, user_country) Redirect(controllers.routes.LoginPage.tfaTOTP()).withSession(request2session).withCookies(authenticator.toCookie) } else if (user.isDefined) { // create session - val authenticator = Authenticator.create(Some(user.get.id), None, email) + val authenticator = Authenticator.create(Some(user.get.id), None, email, user_country) Redirect(toUrl(request2session)). withSession(request2session - SecureSocial.OriginalUrlKey). withCookies(authenticator.toCookie). diff --git a/app/controllers/Registration.scala b/app/controllers/Registration.scala index f7a95a4..29d9bc6 100644 --- a/app/controllers/Registration.scala +++ b/app/controllers/Registration.scala @@ -18,24 +18,29 @@ package controllers import _root_.java.util.UUID import javax.inject.Inject -import play.api.mvc.{ Result, Action, Controller } -import play.api.data._ + +import play.api.mvc.{ Action, Controller, Result } +import play.api.data.{ Mapping, _ } import play.api.data.Forms._ import play.api.data.validation.Constraints._ -import play.api.{ Play, Logger } -import play.api.i18n.{ Lang, MessagesApi, I18nSupport, Messages } +import play.api.{ Logger, Play } +import play.api.i18n.{ I18nSupport, Lang, Messages, MessagesApi } import securesocial.core._ import Play.current import securesocial.core.providers.utils._ import org.joda.time.DateTime + import scala.language.reflectiveCalls import securesocial.core.Token + import scala.Some import securesocial.core.SocialUser import service.{ PGP, txbitsUserService } -import models.{ LogType, LogEvent } +import models.{ LogEvent, LogType } import java.security.SecureRandom +import play.api.data.validation.Constraints + /** * A controller to handle user registration. * @@ -69,7 +74,7 @@ class Registration @Inject() (val messagesApi: MessagesApi) extends Controller w mapping( Email -> email.verifying(nonEmpty) ) // binding - ((email) => StartRegistrationInfo(email)) // unbinding + ((email, user_country) => StartRegistrationInfo(email, user_country)) // unbinding (info => Some(info.email)) ) @@ -140,9 +145,9 @@ class Registration @Inject() (val messagesApi: MessagesApi) extends Controller w } // XXX: copied from ProviderController TODO: fix duplication - def completePasswordAuth[A](id: Long, email: String)(implicit request: play.api.mvc.Request[A]) = { + def completePasswordAuth[A](id: Long, email: String, user_country: String)(implicit request: play.api.mvc.Request[A]) = { import controllers.ProviderController._ - val authenticator = Authenticator.create(Some(id), None, email) + val authenticator = Authenticator.create(Some(id), None, email, user_country) Redirect(toUrl(request2session)).withSession(request2session - SecureSocial.OriginalUrlKey).withCookies(authenticator.toCookie) } @@ -163,22 +168,22 @@ class Registration @Inject() (val messagesApi: MessagesApi) extends Controller w val user = txbitsUserService.create(SocialUser( -1, // this is a placeholder t.email, + t.user_country, 0, //not verified t.language, info.mailingList, false, None, false, - securesocial.core.SecureSocial.currentUser.get.user_country, false - ), info.password, token, info.pgp) + ), t.user_country, info.password, token, info.pgp) txbitsUserService.deleteToken(t.uuid) if (UsernamePasswordProvider.sendWelcomeEmail) { Mailer.sendWelcomeEmail(user) } - globals.logModel.logEvent(LogEvent.fromRequest(Some(user.id), Some(user.email), request, LogType.SignupSuccess)) + globals.logModel.logEvent(LogEvent.fromRequest(Some(user.id), Some(user.email), Some(user.user_country), request, LogType.SignupSuccess)) if (UsernamePasswordProvider.signupSkipLogin) { - completePasswordAuth(user.id, user.email) + completePasswordAuth(user.id, user.email, user.user_country) } else { Redirect(onHandleSignUpGoTo).flashing(Success -> Messages(SignUpDone)).withSession(request2session) } @@ -200,7 +205,7 @@ class Registration @Inject() (val messagesApi: MessagesApi) extends Controller w email => { txbitsUserService.userExists(email) match { case true => { - globals.userModel.trustedActionStart(email, isSignup = false, "") + globals.userModel.trustedActionStart(email, user_country, isSignup = false, "") } case false => { // The user wasn't registered. Oh, well. @@ -223,16 +228,16 @@ class Registration @Inject() (val messagesApi: MessagesApi) extends Controller w BadRequest(views.html.auth.Registration.resetPasswordPage(errors, token)) }, p => { - val toFlash = txbitsUserService.userExists(t.email) match { + val toFlash = txbitsUserService.userExists(t.email, t.user_country) match { case true => { // this should never actually fail because we checked the token already - txbitsUserService.resetPass(t.email, token, p._1) + txbitsUserService.resetPass(t.email, t.user_country, token, p._1) txbitsUserService.deleteToken(token) - Mailer.sendPasswordChangedNotice(t.email, globals.userModel.userPgpByEmail(t.email)) + Mailer.sendPasswordChangedNotice(t.email, globals.userModel.userPgpByEmail(t.email, t.user_country)) Success -> Messages(PasswordUpdated) } case false => { - Logger.error("[securesocial] could not find user with email %s during password reset".format(t.email)) + Logger.error("[securesocial] could not find user with email %s, country %s during password reset".format(t.email, t.user_country)) Error -> Messages(ErrorUpdatingPassword) } } @@ -279,5 +284,5 @@ object Registration { } case class RegistrationInfo(mailingList: Boolean, password: String, pgp: String) - case class StartRegistrationInfo(email: String) + case class StartRegistrationInfo(email: String, user_country: String) } diff --git a/app/models/EngineModel.scala b/app/models/EngineModel.scala index a9e65ae..e537c82 100644 --- a/app/models/EngineModel.scala +++ b/app/models/EngineModel.scala @@ -176,7 +176,13 @@ class EngineModel(val db: String = "default") { row[Option[String]]("email_l1").getOrElse(""), row[Option[String]]("email_l2").getOrElse(""), row[Option[String]]("email_o1").getOrElse(""), - row[Option[String]]("email_o2").getOrElse("") + row[Option[String]]("email_o2").getOrElse(""), + row[Option[String]]("user_country_g1").getOrElse(""), + row[Option[String]]("user_country_g2").getOrElse(""), + row[Option[String]]("user_country_l1").getOrElse(""), + row[Option[String]]("user_country_l2").getOrElse(""), + row[Option[String]]("user_country_o1").getOrElse(""), + row[Option[String]]("user_country_o2").getOrElse("") )).toList } diff --git a/app/models/LogModel.scala b/app/models/LogModel.scala index 05b664c..a73e5ce 100644 --- a/app/models/LogModel.scala +++ b/app/models/LogModel.scala @@ -51,15 +51,15 @@ object LogType extends Enumeration { }*/ } -case class LogEvent(uid: Option[Long], email: Option[String], ip: Option[String], browser_headers: Option[String], browser_id: Option[String], ssl_info: Option[String], created: Option[DateTime], typ: LogType) +case class LogEvent(uid: Option[Long], email: Option[String], user_country: Option[String], ip: Option[String], browser_headers: Option[String], browser_id: Option[String], ssl_info: Option[String], created: Option[DateTime], typ: LogType) object LogEvent { implicit val logEventWrites = Json.writes[LogEvent] - def fromRequest(uid: Option[Long], email: Option[String], request: RequestHeader, typ: LogType) = { - LogEvent(uid, email, Some(LogModel.ipFromRequest(request)), Some(LogModel.headersFromRequest(request)), None, None, None, typ) + def fromRequest(uid: Option[Long], email: Option[String], user_country: Option[String], request: RequestHeader, typ: LogType) = { + LogEvent(uid, email, user_country, Some(LogModel.ipFromRequest(request)), Some(LogModel.headersFromRequest(request)), None, None, None, typ) } } -case class LoginEvent(id: Long, email: Option[String], ip: Option[String], created: Option[DateTime], typ: LogType) +case class LoginEvent(id: Long, email: Option[String], user_country: Option[String], ip: Option[String], created: Option[DateTime], typ: LogType) object LoginEvent { implicit val writes = Json.writes[LoginEvent] @@ -69,7 +69,7 @@ class LogModel(val db: String = "default") { def logEvent(logEvent: LogEvent) = DB.withConnection(db) { implicit c => SQL""" - select * from new_log(${logEvent.uid}, ${logEvent.browser_headers}, ${logEvent.email}, ${logEvent.ssl_info}, ${logEvent.browser_id}, inet(${logEvent.ip}), ${logEvent.typ.toString}) + select * from new_log(${logEvent.uid}, ${logEvent.browser_headers}, ${logEvent.email}, ${logEvent.user_country}, ${logEvent.ssl_info}, ${logEvent.browser_id}, inet(${logEvent.ip}), ${logEvent.typ.toString}) """.execute() } @@ -79,6 +79,7 @@ class LogModel(val db: String = "default") { """().map(row => LoginEvent( row[Long]("id"), row[Option[String]]("email"), + row[Option[String]]("user_country"), row[Option[String]]("ip"), Some(row[DateTime]("created")), LogType.withName(row[Option[String]]("type").getOrElse("other"))) diff --git a/app/models/UserModel.scala b/app/models/UserModel.scala index 71529a4..9246ad6 100644 --- a/app/models/UserModel.scala +++ b/app/models/UserModel.scala @@ -31,16 +31,16 @@ import anorm.JodaParameterMetaData._ class UserModel(val db: String = "default") { - def create(email: String, password: String, onMailingList: Boolean, pgp: Option[String], token: String) = DB.withConnection(db) { implicit c => + def create(email: String, user_country: String, password: String, onMailingList: Boolean, pgp: Option[String], token: String) = DB.withConnection(db) { implicit c => SQL""" - select create_user_complete as id from create_user_complete($email, $password, $onMailingList, $pgp, $token) + select create_user_complete as id from create_user_complete($email, $user_country, $password, $onMailingList, $pgp, $token) """.map(row => row[Option[Long]]("id")).list.head } // insecure version, usable only in tests - def create(email: String, password: String, onMailingList: Boolean) = DB.withConnection(db) { implicit c => + def create(email: String, user_country: String, password: String, onMailingList: Boolean) = DB.withConnection(db) { implicit c => SQL""" - select create_user as id from create_user($email, $password, $onMailingList, null, 'en') + select create_user as id from create_user($email, $user_country, $password, $onMailingList, null, 'en') """.map(row => row[Long]("id")).list.headOption } @@ -50,13 +50,13 @@ class UserModel(val db: String = "default") { new SocialUser( row[Long]("id"), row[String]("email"), + row[String]("user_country"), row[Int]("verification"), row[String]("language"), row[Boolean]("on_mailing_list"), row[Boolean]("tfa_enabled"), row[Option[String]]("pgp"), row[Boolean]("manualauto_mode"), - row[Option[String]]("user_country"), row[Boolean]("docs_verified"), row[Option[String]]("partner"), row[Option[String]]("admin_xx") @@ -64,14 +64,14 @@ class UserModel(val db: String = "default") { ).headOption } - def userExists(email: String): Boolean = DB.withConnection(db) { implicit c => - SQL"select * from user_exists($email)"().map(row => + def userExists(email: String, user_country: String): Boolean = DB.withConnection(db) { implicit c => + SQL"select * from user_exists($email, $user_country)"().map(row => row[Boolean]("user_exists") ).head } - def userHasTotp(email: String): Boolean = DB.withConnection(db) { implicit c => - SQL"select * from user_has_totp($email)"().map(row => + def userHasTotp(email: String, user_country: String): Boolean = DB.withConnection(db) { implicit c => + SQL"select * from user_has_totp($email, $user_country)"().map(row => row[Option[Boolean]]("user_has_totp").getOrElse(false) ).head } @@ -84,64 +84,64 @@ class UserModel(val db: String = "default") { ).head } - def totpLoginStep2(email: String, totpHash: String, totpToken: String, browserHeaders: String, ip: String): Option[SocialUser] = DB.withConnection(db) { implicit c => + def totpLoginStep2(email: String, user_country: String, totpHash: String, totpToken: String, browserHeaders: String, ip: String): Option[SocialUser] = DB.withConnection(db) { implicit c => SQL""" select * from totp_login_step2($email, $totpHash, ${safeToInt(totpToken)}, $browserHeaders, inet($ip)) """().map(row => (row[Option[Long]]("id"), row[Option[String]]("email"), + row[String]("user_country"), row[Option[Int]]("verification"), row[Option[Boolean]]("on_mailing_list"), row[Option[Boolean]]("tfa_enabled"), row[Option[String]]("pgp"), row[String]("language"), row[Option[Boolean]]("manualauto_mode"), - row[Option[String]]("user_country"), row[Option[Boolean]]("docs_verified"), row[Option[String]]("partner")) match { case (Some(id: Long), Some(email: String), + user_country: String, Some(verification: Int), Some(on_mailing_list: Boolean), Some(tfa_enabled: Boolean), pgp: Option[String], language: String, manualauto_mode: Option[Boolean], - user_country: Option[String], docs_verified: Option[Boolean], partner: Option[String]) => - Some(SocialUser(id, email, verification, language, on_mailing_list, tfa_enabled, pgp, manualauto_mode.getOrElse(false), user_country, docs_verified.getOrElse(false), partner)) + Some(SocialUser(id, email, user_country, verification, language, on_mailing_list, tfa_enabled, pgp, manualauto_mode.getOrElse(false), docs_verified.getOrElse(false), partner)) case _ => None } ).head } - def findUserByEmailAndPassword(email: String, password: String, user_country: String, browserHeaders: String, ip: String): Option[SocialUser] = DB.withConnection(db) { implicit c => + def findUserByEmailAndPassword(email: String, user_country: String, password: String, browserHeaders: String, ip: String): Option[SocialUser] = DB.withConnection(db) { implicit c => SQL""" - select * from find_user_by_email_and_password($email, $password, $user_country, $browserHeaders, inet($ip)) + select * from find_user_by_email_and_password($email, $user_country, $password, $browserHeaders, inet($ip)) """().map(row => (row[Option[Long]]("id"), row[Option[String]]("email"), + row[Option[String]]("user_country"), row[Option[Int]]("verification"), row[Option[Boolean]]("on_mailing_list"), row[Option[Boolean]]("tfa_enabled"), row[Option[String]]("pgp"), row[Option[String]]("language"), row[Option[Boolean]]("manualauto_mode"), - row[Option[String]]("user_country"), row[Option[Boolean]]("docs_verified"), row[Option[String]]("partner")) match { case (Some(id: Long), Some(email: String), + Some(user_country: String), Some(verification: Int), Some(on_mailing_list: Boolean), Some(tfa_enabled: Boolean), pgp: Option[String], Some(language: String), Some(manualauto_mode: Boolean), - user_country: Option[String], Some(docs_verified: Boolean), partner: Option[String]) => - Some(SocialUser(id, email, verification, language, on_mailing_list, tfa_enabled, pgp, manualauto_mode, user_country, docs_verified, partner)) + Some(SocialUser(id, email, user_country, verification, language, on_mailing_list, tfa_enabled, pgp, manualauto_mode, docs_verified, partner)) case _ => None } @@ -152,7 +152,7 @@ class UserModel(val db: String = "default") { SQL""" select * from find_token($token) """().map(row => - Token(token, row[String]("email"), row[DateTime]("creation"), row[DateTime]("expiration"), row[Boolean]("is_signup"), row[String]("language")) + Token(token, row[String]("email"), row[String]("user_country"), row[DateTime]("creation"), row[DateTime]("expiration"), row[Boolean]("is_signup"), row[String]("language")) ).headOption } @@ -172,8 +172,8 @@ class UserModel(val db: String = "default") { """.execute() } - def saveUser(id: Long, email: String, onMailingList: Boolean) = DB.withConnection(db) { implicit c => - SQL"select * from update_user($id, $email, $onMailingList)".execute() + def saveUser(id: Long, email: String, user_country: String, onMailingList: Boolean) = DB.withConnection(db) { implicit c => + SQL"select * from update_user($id, $email, $user_country, $onMailingList)".execute() } def userChangePass(id: Long, oldPassword: String, newPassword: String) = DB.withConnection(db) { implicit c => @@ -192,9 +192,9 @@ class UserModel(val db: String = "default") { ).head } - def trustedActionStart(email: String, isSignup: Boolean, language: String) = DB.withConnection(db) { implicit c => + def trustedActionStart(email: String, user_country: String, isSignup: Boolean, language: String) = DB.withConnection(db) { implicit c => SQL""" - select trusted_action_start as success from trusted_action_start($email, $isSignup, $language) + select trusted_action_start as success from trusted_action_start($email, $user_country, $isSignup, $language) """().map(row => row[Boolean]("success") ).head @@ -275,9 +275,9 @@ class UserModel(val db: String = "default") { ).head } - def userPgpByEmail(email: String) = DB.withConnection(db) { implicit c => + def userPgpByEmail(email: String, user_country: String) = DB.withConnection(db) { implicit c => SQL""" - select * from user_pgp_by_email($email) + select * from user_pgp_by_email($email, $user_country) """().map(row => row[Option[String]]("pgp") ).head @@ -358,9 +358,9 @@ class UserModel(val db: String = "default") { """.execute() } - def save_admins(country: Option[String], admin_g1: Option[String], admin_g2: Option[String], admin_l1: Option[String], admin_l2: Option[String], admin_o1: Option[String], admin_o2: Option[String]) = DB.withConnection(db) { implicit c => + def save_admins(country: Option[String], email_g1: Option[String], email_g2: Option[String], email_l1: Option[String], email_l2: Option[String], email_o1: Option[String], email_o2: Option[String], user_country_g1: Option[String], user_country_g2: Option[String], user_country_l1: Option[String], user_country_l2: Option[String], user_country_o1: Option[String], user_country_o2: Option[String]) = DB.withConnection(db) { implicit c => SQL""" - select save_admins as success from save_admins(${country.get}, ${admin_g1.get}, ${admin_g2.get}, ${admin_l1.get}, ${admin_l2.get}, ${admin_o1.get}, ${admin_o2.get}) + select save_admins as success from save_admins(${country.get}, ${email_g1.get}, ${email_g2.get}, ${email_l1.get}, ${email_l2.get}, ${email_o1.get}, ${email_o2.get}, {user_country_g1.get}, ${user_country_g2.get}, ${user_country_l1.get}, ${user_country_l2.get}, ${user_country_o1.get}, ${user_country_o2.get}) """.execute() } diff --git a/app/securesocial/core/Authenticator.scala b/app/securesocial/core/Authenticator.scala index 0646648..8f9b378 100644 --- a/app/securesocial/core/Authenticator.scala +++ b/app/securesocial/core/Authenticator.scala @@ -36,7 +36,7 @@ import org.apache.commons.codec.binary.Base32 * @param expirationDate The expiration time */ case class Authenticator(id: String, uid: Option[Long], creationDate: DateTime, - lastUsed: DateTime, expirationDate: DateTime, totpSecret: Option[String], email: String) { + lastUsed: DateTime, expirationDate: DateTime, totpSecret: Option[String], email: String, user_country: String) { /** * Creates a cookie representing this authenticator @@ -174,11 +174,11 @@ object Authenticator { * @param totpSecret the secret to complete two factor auth or None * @return an authenticator or error if there was a problem creating it */ - def create(uid: Option[Long], totpSecret: Option[String], email: String): Authenticator = { + def create(uid: Option[Long], totpSecret: Option[String], email: String, user_country: String): Authenticator = { val id = IdGenerator.generate val now = DateTime.now() val expirationDate = now.plusMinutes(absoluteTimeout) - val authenticator = Authenticator(id, uid, now, now, expirationDate, totpSecret, email) + val authenticator = Authenticator(id, uid, now, now, expirationDate, totpSecret, email, user_country) AuthenticatorStore.save(authenticator) authenticator } diff --git a/app/securesocial/core/SecureSocial.scala b/app/securesocial/core/SecureSocial.scala index 03ffc7b..e67e007 100644 --- a/app/securesocial/core/SecureSocial.scala +++ b/app/securesocial/core/SecureSocial.scala @@ -172,7 +172,7 @@ object SecureSocial { case Some(a) => { if (!a.isValid) { Authenticator.delete(a.id) - globals.logModel.logEvent(LogEvent.fromRequest(a.uid, None, request, LogType.SessionExpired)) + globals.logModel.logEvent(LogEvent.fromRequest(a.uid, None, None, request, LogType.SessionExpired)) None } else { Some(a) diff --git a/app/securesocial/core/SocialUser.scala b/app/securesocial/core/SocialUser.scala index 48c4139..aa76a1d 100644 --- a/app/securesocial/core/SocialUser.scala +++ b/app/securesocial/core/SocialUser.scala @@ -21,14 +21,16 @@ import play.api.libs.json.{ JsValue, Json, JsObject, Writes } /** * An implementation of Identity. Used by SecureSocial to gather user information when users sign up and/or sign in. */ -case class SocialUser(id: Long, email: String, verification: Int, language: String, onMailingList: Boolean, TFAEnabled: Boolean = false, pgp: Option[String] = None, manualauto_mode: Boolean, user_country: Option[String] = None, docs_verified: Boolean, partner: Option[String] = None, admin_xx: Option[String] = None) + +// Next line should not be br but default_country +case class SocialUser(id: Long, email: String, user_country: String, verification: Int, language: String, onMailingList: Boolean, TFAEnabled: Boolean = false, pgp: Option[String] = None, manualauto_mode: Boolean, docs_verified: Boolean, partner: Option[String] = None, admin_xx: Option[String] = None) object SocialUser { implicit def writes = new Writes[SocialUser] { def writes(u: SocialUser): JsValue = { // include everything except the id - Json.obj("email" -> u.email, "verification" -> u.verification, "onMailingList" -> u.onMailingList, - "TFAEnabled" -> u.TFAEnabled, "pgp" -> u.pgp, "language" -> u.language, "manualauto_mode" -> u.manualauto_mode, "user_country" -> u.user_country, + Json.obj("email" -> u.email, "user_country" -> u.user_country, "verification" -> u.verification, "onMailingList" -> u.onMailingList, + "TFAEnabled" -> u.TFAEnabled, "pgp" -> u.pgp, "language" -> u.language, "manualauto_mode" -> u.manualauto_mode, "docs_verified" -> u.docs_verified, "partner" -> u.partner, "admin_xx" -> u.admin_xx) } } diff --git a/app/securesocial/core/UsernamePasswordProvider.scala b/app/securesocial/core/UsernamePasswordProvider.scala index 28fee13..6c7a1dd 100644 --- a/app/securesocial/core/UsernamePasswordProvider.scala +++ b/app/securesocial/core/UsernamePasswordProvider.scala @@ -70,10 +70,11 @@ object UsernamePasswordProvider { * * @param uuid the token id * @param email the user email + * // * @param user_country the country that system is to work * @param creationTime the creation time * @param expirationTime the expiration time * @param isSignUp a boolean indicating wether the token was created for a sign up action or not */ -case class Token(uuid: String, email: String, creationTime: DateTime, expirationTime: DateTime, isSignUp: Boolean, language: String) { +case class Token(uuid: String, email: String, user_country: String, creationTime: DateTime, expirationTime: DateTime, isSignUp: Boolean, language: String) { def isExpired = expirationTime.isBeforeNow } diff --git a/app/service/txbitsUserService.scala b/app/service/txbitsUserService.scala index f74a81f..73aa3f5 100644 --- a/app/service/txbitsUserService.scala +++ b/app/service/txbitsUserService.scala @@ -32,13 +32,13 @@ object txbitsUserService { globals.userModel.findUserById(id) } - def userExists(email: String): Boolean = { - globals.userModel.userExists(email) + def userExists(email: String, user_country: String): Boolean = { + globals.userModel.userExists(email, user_country) } - def create(user: SocialUser, password: String, token: String, pgp: String): SocialUser = { + def create(user: SocialUser, user_country: String, password: String, token: String, pgp: String): SocialUser = { val pgp_key = PGP.parsePublicKey(pgp).map(_._2) - val user_id = globals.userModel.create(user.email, password, user.onMailingList, pgp_key, token) + val user_id = globals.userModel.create(user.email, user_country, password, user.onMailingList, pgp_key, token) user_id match { case Some(id) => { @@ -49,13 +49,13 @@ object txbitsUserService { } def save(user: SocialUser): SocialUser = { - globals.userModel.saveUser(user.id, user.email, user.onMailingList) + globals.userModel.saveUser(user.id, user.email, user.user_country, user.onMailingList) user } // this function requires higher database privileges - def resetPass(email: String, token: String, password: String) { - globals.userModel.userResetPassComplete(email, token, password) + def resetPass(email: String, user_country: String, token: String, password: String) { + globals.userModel.userResetPassComplete(email, user_country, token, password) } /** diff --git a/app/usertrust/UserTrustModel.scala b/app/usertrust/UserTrustModel.scala index 3e1214a..ae5db1e 100644 --- a/app/usertrust/UserTrustModel.scala +++ b/app/usertrust/UserTrustModel.scala @@ -25,19 +25,19 @@ import anorm._ class UserTrustModel(val db: String = "default") { def getTrustedActionRequests = DB.withConnection(db) { implicit c => - SQL"""select email, is_signup, language from trusted_action_requests"""().map(row => - (row[String]("email"), row[Boolean]("is_signup"), row[String]("language")) + SQL"""select email, user_country, is_signup, language from trusted_action_requests"""().map(row => + (row[String]("email"), row[String]("user_country"), row[Boolean]("is_signup"), row[String]("language")) ).toList } - def trustedActionFinish(email: String, is_signup: Boolean) = DB.withConnection(db) { implicit c => - SQL"""delete from trusted_action_requests where email = $email and is_signup = $is_signup""".execute + def trustedActionFinish(email: String, user_country: String, is_signup: Boolean) = DB.withConnection(db) { implicit c => + SQL"""delete from trusted_action_requests where email = $email and and user_country = $user_country and is_signup = $is_signup""".execute } def saveToken(token: Token) = DB.withConnection(db) { implicit c => SQL""" - insert into tokens (token, email, creation, expiration, is_signup, language) - values (${token.uuid}, ${token.email}, ${new Timestamp(token.creationTime.getMillis)}, ${new Timestamp(token.expirationTime.getMillis)}, ${token.isSignUp}, ${token.language}) + insert into tokens (token, email, user_country, creation, expiration, is_signup, language) + values (${token.uuid}, ${token.email}, ${token.user_country}, ${new Timestamp(token.creationTime.getMillis)}, ${new Timestamp(token.expirationTime.getMillis)}, ${token.isSignUp}, ${token.language}) """.execute } } diff --git a/app/usertrust/UserTrustService.scala b/app/usertrust/UserTrustService.scala index fc65ad6..51d0b3f 100644 --- a/app/usertrust/UserTrustService.scala +++ b/app/usertrust/UserTrustService.scala @@ -43,7 +43,7 @@ class UserTrustService(val model: UserTrustModel) extends Actor { // Warning: It is not safe to have two user trust services running at the same time def processTrustedActionRequests() { - for ((email, is_signup, language) <- model.getTrustedActionRequests) { + for ((email, user_country, is_signup, language) <- model.getTrustedActionRequests) { // XXX: temporary hack to make languages work in emails implicit val messages = new Messages(Lang.get(language).getOrElse(Lang.defaultLang), new DefaultMessagesApi(play.api.Environment.simple(new File("."), Mode.Prod), @@ -58,29 +58,31 @@ class UserTrustService(val model: UserTrustModel) extends Actor { txbitsUserService.userExists(email) match { case true => { // user signed up already, send an email offering to login/recover password - Mailer.sendAlreadyRegisteredEmail(email, globals.userModel.userPgpByEmail(email)) + Mailer.sendAlreadyRegisteredEmail(email, globals.userModel.userPgpByEmail(email, user_country)) } case false => { - val token = createToken(email, isSignUp = is_signup, language) + val token = createToken(email, user_country, isSignUp = is_signup, language) Mailer.sendSignUpEmail(email, token) } } } else { // create and save token - val token = createToken(email, isSignUp = is_signup, language) - Mailer.sendPasswordResetEmail(email, token, globals.userModel.userPgpByEmail(email)) + val token = createToken(email, user_country, isSignUp = is_signup, language) + Mailer.sendPasswordResetEmail(email, token, globals.userModel.userPgpByEmail(email, user_country)) } // remove the token from the queue - model.trustedActionFinish(email, is_signup) + model.trustedActionFinish(email, user_country, is_signup) } } - private def createToken(email: String, isSignUp: Boolean, language: String) = { + private def createToken(email: String, user_country: String, isSignUp: Boolean, language: String) = { val tokenId = IdGenerator.generateEmailToken val now = DateTime.now val token = Token( - tokenId, email, + tokenId, + email, + user_country, now, now.plusMinutes(TokenDuration), isSignUp = isSignUp, diff --git a/app/views/_global_chrome.scala.html b/app/views/_global_chrome.scala.html index 331217a..b717825 100644 --- a/app/views/_global_chrome.scala.html +++ b/app/views/_global_chrome.scala.html @@ -36,7 +36,7 @@ - + @content diff --git a/app/views/administrator/management.scala.html b/app/views/administrator/management.scala.html index acfedcb..681a915 100644 --- a/app/views/administrator/management.scala.html +++ b/app/views/administrator/management.scala.html @@ -20,7 +20,6 @@

  @Messages("directpay.navigation.admi -
@@ -91,46 +90,54 @@

  @Messages("dir
+

+
} else {
{{email_g1}}
+
{{user_country_g1}}
{{email_g2}}
+
{{user_country_g2}}
}
+
+
+
+
diff --git a/app/views/content/_chrome.scala.html b/app/views/content/_chrome.scala.html index 87b1ce3..93df9aa 100644 --- a/app/views/content/_chrome.scala.html +++ b/app/views/content/_chrome.scala.html @@ -51,12 +51,12 @@ } @@ -65,7 +65,7 @@