|
1 | | -/* |
2 | | - * Copyright 2019 New Vector Ltd |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | - |
17 | | -package im.vector.app.features.login.terms |
18 | | - |
19 | | -import org.matrix.android.sdk.api.auth.data.LocalizedFlowDataLoginTerms |
20 | | -import org.matrix.android.sdk.api.auth.registration.TermPolicies |
21 | | - |
22 | | -/** |
23 | | - * This method extract the policies from the login terms parameter, regarding the user language. |
24 | | - * For each policy, if user language is not found, the default language is used and if not found, the first url and name are used (not predictable) |
25 | | - * |
26 | | - * Example of Data: |
27 | | - * <pre> |
28 | | - * "m.login.terms": { |
29 | | - * "policies": { |
30 | | - * "privacy_policy": { |
31 | | - * "version": "1.0", |
32 | | - * "en": { |
33 | | - * "url": "http:\/\/matrix.org\/_matrix\/consent?v=1.0", |
34 | | - * "name": "Terms and Conditions" |
35 | | - * } |
36 | | - * } |
37 | | - * } |
38 | | - * } |
39 | | - *</pre> |
40 | | - * |
41 | | - * @param userLanguage the user language |
42 | | - * @param defaultLanguage the default language to use if the user language is not found for a policy in registrationFlowResponse |
43 | | - */ |
44 | | -fun TermPolicies.toLocalizedLoginTerms(userLanguage: String, |
45 | | - defaultLanguage: String = "en"): List<LocalizedFlowDataLoginTerms> { |
46 | | - val result = ArrayList<LocalizedFlowDataLoginTerms>() |
47 | | - |
48 | | - val policies = get("policies") |
49 | | - if (policies is Map<*, *>) { |
50 | | - policies.keys.forEach { policyName -> |
51 | | - val localizedFlowDataLoginTermsPolicyName = policyName as String |
52 | | - var localizedFlowDataLoginTermsVersion: String? = null |
53 | | - var localizedFlowDataLoginTermsLocalizedUrl: String? = null |
54 | | - var localizedFlowDataLoginTermsLocalizedName: String? = null |
55 | | - |
56 | | - val policy = policies[policyName] |
57 | | - |
58 | | - // Enter this policy |
59 | | - if (policy is Map<*, *>) { |
60 | | - // Version |
61 | | - localizedFlowDataLoginTermsVersion = policy["version"] as String? |
62 | | - |
63 | | - var userLanguageUrlAndName: UrlAndName? = null |
64 | | - var defaultLanguageUrlAndName: UrlAndName? = null |
65 | | - var firstUrlAndName: UrlAndName? = null |
66 | | - |
67 | | - // Search for language |
68 | | - policy.keys.forEach { policyKey -> |
69 | | - when (policyKey) { |
70 | | - "version" -> Unit // Ignore |
71 | | - userLanguage -> { |
72 | | - // We found the data for the user language |
73 | | - userLanguageUrlAndName = extractUrlAndName(policy[policyKey]) |
74 | | - } |
75 | | - defaultLanguage -> { |
76 | | - // We found default language |
77 | | - defaultLanguageUrlAndName = extractUrlAndName(policy[policyKey]) |
78 | | - } |
79 | | - else -> { |
80 | | - if (firstUrlAndName == null) { |
81 | | - // Get at least some data |
82 | | - firstUrlAndName = extractUrlAndName(policy[policyKey]) |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - // Copy found language data by priority |
89 | | - when { |
90 | | - userLanguageUrlAndName != null -> { |
91 | | - localizedFlowDataLoginTermsLocalizedUrl = userLanguageUrlAndName!!.url |
92 | | - localizedFlowDataLoginTermsLocalizedName = userLanguageUrlAndName!!.name |
93 | | - } |
94 | | - defaultLanguageUrlAndName != null -> { |
95 | | - localizedFlowDataLoginTermsLocalizedUrl = defaultLanguageUrlAndName!!.url |
96 | | - localizedFlowDataLoginTermsLocalizedName = defaultLanguageUrlAndName!!.name |
97 | | - } |
98 | | - firstUrlAndName != null -> { |
99 | | - localizedFlowDataLoginTermsLocalizedUrl = firstUrlAndName!!.url |
100 | | - localizedFlowDataLoginTermsLocalizedName = firstUrlAndName!!.name |
101 | | - } |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - result.add(LocalizedFlowDataLoginTerms( |
106 | | - policyName = localizedFlowDataLoginTermsPolicyName, |
107 | | - version = localizedFlowDataLoginTermsVersion, |
108 | | - localizedUrl = localizedFlowDataLoginTermsLocalizedUrl, |
109 | | - localizedName = localizedFlowDataLoginTermsLocalizedName |
110 | | - )) |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - return result |
115 | | -} |
116 | | - |
117 | | -private fun extractUrlAndName(policyData: Any?): UrlAndName? { |
118 | | - if (policyData is Map<*, *>) { |
119 | | - val url = policyData["url"] as String? |
120 | | - val name = policyData["name"] as String? |
121 | | - |
122 | | - if (url != null && name != null) { |
123 | | - return UrlAndName(url, name) |
124 | | - } |
125 | | - } |
126 | | - return null |
127 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2020 The Matrix.org Foundation C.I.C. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.matrix.android.sdk.api.auth |
| 18 | + |
| 19 | +import org.matrix.android.sdk.api.auth.data.LocalizedFlowDataLoginTerms |
| 20 | +import org.matrix.android.sdk.api.auth.registration.TermPolicies |
| 21 | + |
| 22 | +/** |
| 23 | + * This method extract the policies from the login terms parameter, regarding the user language. |
| 24 | + * For each policy, if user language is not found, the default language is used and if not found, the first url and name are used (not predictable) |
| 25 | + * |
| 26 | + * Example of Data: |
| 27 | + * <pre> |
| 28 | + * "m.login.terms": { |
| 29 | + * "policies": { |
| 30 | + * "privacy_policy": { |
| 31 | + * "version": "1.0", |
| 32 | + * "en": { |
| 33 | + * "url": "http:\/\/matrix.org\/_matrix\/consent?v=1.0", |
| 34 | + * "name": "Terms and Conditions" |
| 35 | + * } |
| 36 | + * } |
| 37 | + * } |
| 38 | + * } |
| 39 | + *</pre> |
| 40 | + * |
| 41 | + * @param userLanguage the user language |
| 42 | + * @param defaultLanguage the default language to use if the user language is not found for a policy in registrationFlowResponse |
| 43 | + */ |
| 44 | +fun TermPolicies.toLocalizedLoginTerms(userLanguage: String, |
| 45 | + defaultLanguage: String = "en"): List<LocalizedFlowDataLoginTerms> { |
| 46 | + val result = ArrayList<LocalizedFlowDataLoginTerms>() |
| 47 | + |
| 48 | + val policies = get("policies") |
| 49 | + if (policies is Map<*, *>) { |
| 50 | + policies.keys.forEach { policyName -> |
| 51 | + val localizedFlowDataLoginTermsPolicyName = policyName as String |
| 52 | + var localizedFlowDataLoginTermsVersion: String? = null |
| 53 | + var localizedFlowDataLoginTermsLocalizedUrl: String? = null |
| 54 | + var localizedFlowDataLoginTermsLocalizedName: String? = null |
| 55 | + |
| 56 | + val policy = policies[policyName] |
| 57 | + |
| 58 | + // Enter this policy |
| 59 | + if (policy is Map<*, *>) { |
| 60 | + // Version |
| 61 | + localizedFlowDataLoginTermsVersion = policy["version"] as String? |
| 62 | + |
| 63 | + var userLanguageUrlAndName: UrlAndName? = null |
| 64 | + var defaultLanguageUrlAndName: UrlAndName? = null |
| 65 | + var firstUrlAndName: UrlAndName? = null |
| 66 | + |
| 67 | + // Search for language |
| 68 | + policy.keys.forEach { policyKey -> |
| 69 | + when (policyKey) { |
| 70 | + "version" -> Unit // Ignore |
| 71 | + userLanguage -> { |
| 72 | + // We found the data for the user language |
| 73 | + userLanguageUrlAndName = extractUrlAndName(policy[policyKey]) |
| 74 | + } |
| 75 | + defaultLanguage -> { |
| 76 | + // We found default language |
| 77 | + defaultLanguageUrlAndName = extractUrlAndName(policy[policyKey]) |
| 78 | + } |
| 79 | + else -> { |
| 80 | + if (firstUrlAndName == null) { |
| 81 | + // Get at least some data |
| 82 | + firstUrlAndName = extractUrlAndName(policy[policyKey]) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Copy found language data by priority |
| 89 | + when { |
| 90 | + userLanguageUrlAndName != null -> { |
| 91 | + localizedFlowDataLoginTermsLocalizedUrl = userLanguageUrlAndName!!.url |
| 92 | + localizedFlowDataLoginTermsLocalizedName = userLanguageUrlAndName!!.name |
| 93 | + } |
| 94 | + defaultLanguageUrlAndName != null -> { |
| 95 | + localizedFlowDataLoginTermsLocalizedUrl = defaultLanguageUrlAndName!!.url |
| 96 | + localizedFlowDataLoginTermsLocalizedName = defaultLanguageUrlAndName!!.name |
| 97 | + } |
| 98 | + firstUrlAndName != null -> { |
| 99 | + localizedFlowDataLoginTermsLocalizedUrl = firstUrlAndName!!.url |
| 100 | + localizedFlowDataLoginTermsLocalizedName = firstUrlAndName!!.name |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + result.add(LocalizedFlowDataLoginTerms( |
| 106 | + policyName = localizedFlowDataLoginTermsPolicyName, |
| 107 | + version = localizedFlowDataLoginTermsVersion, |
| 108 | + localizedUrl = localizedFlowDataLoginTermsLocalizedUrl, |
| 109 | + localizedName = localizedFlowDataLoginTermsLocalizedName |
| 110 | + )) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return result |
| 115 | +} |
| 116 | + |
| 117 | +private fun extractUrlAndName(policyData: Any?): UrlAndName? { |
| 118 | + if (policyData is Map<*, *>) { |
| 119 | + val url = policyData["url"] as String? |
| 120 | + val name = policyData["name"] as String? |
| 121 | + |
| 122 | + if (url != null && name != null) { |
| 123 | + return UrlAndName(url, name) |
| 124 | + } |
| 125 | + } |
| 126 | + return null |
| 127 | +} |
0 commit comments