Skip to content

Commit db5c56a

Browse files
authored
Merge pull request #1729 from OneSignal/user-model/email-sms-validation
[User Model] Add validation logic to add/remove email & sms subscriptions
2 parents 3e3011c + 454bf34 commit db5c56a

File tree

5 files changed

+111
-21
lines changed

5 files changed

+111
-21
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ private void setupEmailLayout() {
526526
@Override
527527
public void onSuccess(String value) {
528528
if (value != null && !value.isEmpty()) {
529-
OneSignal.getUser().addEmailSubscription(value);
529+
OneSignal.getUser().addEmail(value);
530530
emailArrayList.add(new DummySubscription(value));
531531
toaster.makeCustomViewToast("Added email " + value, ToastType.SUCCESS);
532532
}
@@ -549,7 +549,7 @@ private void setupSMSLayout() {
549549
@Override
550550
public void onSuccess(String value) {
551551
if (value != null && !value.isEmpty()) {
552-
OneSignal.getUser().addSmsSubscription(value);
552+
OneSignal.getUser().addSms(value);
553553
smsArrayList.add(new DummySubscription(value));
554554
toaster.makeCustomViewToast("Added SMS " + value, ToastType.SUCCESS);
555555
}
@@ -630,7 +630,7 @@ private void setupEmailRecyclerView() {
630630
emailsRecyclerView.setLayoutManager(linearLayoutManager);
631631
emailsRecyclerViewAdapter = new SubscriptionRecyclerViewAdapter(context, emailArrayList, value -> {
632632
String email = ((DummySubscription)value).getId();
633-
OneSignal.getUser().removeEmailSubscription(email);
633+
OneSignal.getUser().removeEmail(email);
634634
emailArrayList.remove(value);
635635
refreshEmailRecyclerView();
636636
toaster.makeCustomViewToast("Deleted email " + email, ToastType.SUCCESS);
@@ -656,7 +656,7 @@ private void setupSMSRecyclerView() {
656656
smssRecyclerView.setLayoutManager(linearLayoutManager);
657657
smssRecyclerViewAdapter = new SubscriptionRecyclerViewAdapter(context, smsArrayList, value -> {
658658
String number = ((DummySubscription)value).getId();
659-
OneSignal.getUser().removeSmsSubscription(number);
659+
OneSignal.getUser().removeSms(number);
660660
smsArrayList.remove(value);
661661
refreshSMSRecyclerView();
662662
toaster.makeCustomViewToast("Deleted SMS " + number, ToastType.SUCCESS);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
package com.onesignal.common
22

3+
import java.util.regex.Pattern
4+
35
object OneSignalUtils {
6+
@JvmStatic
47
var sdkType = "native"
8+
9+
@JvmStatic
510
val sdkVersion: String = "050000"
11+
12+
fun isValidEmail(email: String): Boolean {
13+
if (email.isEmpty())
14+
return false
15+
16+
val emRegex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"
17+
val pattern: Pattern = Pattern.compile(emRegex)
18+
return pattern.matcher(email).matches()
19+
}
20+
21+
fun isValidPhoneNumber(number: String): Boolean {
22+
if (number.isEmpty())
23+
return false
24+
25+
val emRegex = "^\\+?[1-9]\\d{1,14}\$"
26+
val pattern: Pattern = Pattern.compile(emRegex)
27+
return pattern.matcher(number).matches()
28+
}
629
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/IUserManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ interface IUserManager {
7070
*
7171
* @param email The email address that the current user has subscribed for.
7272
*/
73-
fun addEmailSubscription(email: String)
73+
fun addEmail(email: String)
7474

7575
/**
7676
* Remove an email subscription from the current user.
7777
*
7878
* @param email The email address that the current user was subscribed for, and should no longer be.
7979
*/
80-
fun removeEmailSubscription(email: String)
80+
fun removeEmail(email: String)
8181

8282
/**
8383
* Add a new SMS subscription to the current user.
8484
*
8585
* @param sms The phone number that the current user has subscribed for, in [E.164](https://documentation.onesignal.com/docs/sms-faq#what-is-the-e164-format) format.
8686
*/
87-
fun addSmsSubscription(sms: String)
87+
fun addSms(sms: String)
8888

8989
/**
9090
* Remove an SMS subscription from the current user.
9191
*
9292
* @param sms The sms address that the current user was subscribed for, and should no longer be.
9393
*/
94-
fun removeSmsSubscription(sms: String)
94+
fun removeSms(sms: String)
9595

9696
/**
9797
* Add a tag for the current user. Tags are key:value pairs used as building blocks

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal
22

3+
import com.onesignal.common.OneSignalUtils
34
import com.onesignal.core.internal.language.ILanguageContext
45
import com.onesignal.debug.LogLevel
56
import com.onesignal.debug.internal.logging.Logging
@@ -48,8 +49,12 @@ internal open class UserManager(
4849
override fun addAlias(label: String, id: String) {
4950
Logging.log(LogLevel.DEBUG, "setAlias(label: $label, id: $id)")
5051

52+
if(label.isEmpty()) {
53+
throw Exception("Cannot add empty alias")
54+
}
55+
5156
if (label == IdentityConstants.ONESIGNAL_ID) {
52-
throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias")
57+
throw Exception("Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias")
5358
}
5459

5560
_identityModel[label] = id
@@ -58,8 +63,14 @@ internal open class UserManager(
5863
override fun addAliases(aliases: Map<String, String>) {
5964
Logging.log(LogLevel.DEBUG, "addAliases(aliases: $aliases")
6065

61-
if (aliases.keys.any { it == IdentityConstants.ONESIGNAL_ID }) {
62-
throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias")
66+
aliases.forEach {
67+
if(it.key.isEmpty()) {
68+
throw Exception("Cannot add empty alias")
69+
}
70+
71+
if (it.key == IdentityConstants.ONESIGNAL_ID) {
72+
throw Exception("Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias")
73+
}
6374
}
6475

6576
aliases.forEach {
@@ -70,6 +81,10 @@ internal open class UserManager(
7081
override fun removeAlias(label: String) {
7182
Logging.log(LogLevel.DEBUG, "removeAlias(label: $label)")
7283

84+
if(label.isEmpty()) {
85+
throw Exception("Cannot remove empty alias")
86+
}
87+
7388
if (label == IdentityConstants.ONESIGNAL_ID) {
7489
throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias")
7590
}
@@ -80,52 +95,104 @@ internal open class UserManager(
8095
override fun removeAliases(labels: Collection<String>) {
8196
Logging.log(LogLevel.DEBUG, "removeAliases(labels: $labels)")
8297

98+
labels.forEach {
99+
if(it.isEmpty()) {
100+
throw Exception("Cannot remove empty alias")
101+
}
102+
103+
if (it == IdentityConstants.ONESIGNAL_ID) {
104+
throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias")
105+
}
106+
}
107+
83108
labels.forEach {
84109
_identityModel.remove(it)
85110
}
86111
}
87112

88-
override fun addEmailSubscription(email: String) {
89-
Logging.log(LogLevel.DEBUG, "addEmailSubscription(email: $email)")
113+
override fun addEmail(email: String) {
114+
Logging.log(LogLevel.DEBUG, "addEmail(email: $email)")
115+
116+
if(!OneSignalUtils.isValidEmail(email)) {
117+
throw Exception("Cannot add invalid email address as subscription: $email")
118+
}
119+
90120
_subscriptionManager.addEmailSubscription(email)
91121
}
92122

93-
override fun removeEmailSubscription(email: String) {
94-
Logging.log(LogLevel.DEBUG, "removeEmailSubscription(email: $email)")
123+
override fun removeEmail(email: String) {
124+
Logging.log(LogLevel.DEBUG, "removeEmail(email: $email)")
125+
126+
if(!OneSignalUtils.isValidEmail(email)) {
127+
throw Exception("Cannot remove invalid email address as subscription: $email")
128+
}
129+
95130
_subscriptionManager.removeEmailSubscription(email)
96131
}
97132

98-
override fun addSmsSubscription(sms: String) {
99-
Logging.log(LogLevel.DEBUG, "addSmsSubscription(sms: $sms)")
133+
override fun addSms(sms: String) {
134+
Logging.log(LogLevel.DEBUG, "addSms(sms: $sms)")
135+
136+
if(!OneSignalUtils.isValidPhoneNumber(sms)) {
137+
throw Exception("Cannot add invalid sms number as subscription: $sms")
138+
}
139+
100140
_subscriptionManager.addSmsSubscription(sms)
101141
}
102142

103-
override fun removeSmsSubscription(sms: String) {
104-
Logging.log(LogLevel.DEBUG, "removeSmsSubscription(sms: $sms)")
143+
override fun removeSms(sms: String) {
144+
Logging.log(LogLevel.DEBUG, "removeSms(sms: $sms)")
145+
146+
if(!OneSignalUtils.isValidPhoneNumber(sms)) {
147+
throw Exception("Cannot remove invalid sms number as subscription: $sms")
148+
}
149+
105150
_subscriptionManager.removeSmsSubscription(sms)
106151
}
107152

108153
override fun addTag(key: String, value: String) {
109154
Logging.log(LogLevel.DEBUG, "setTag(key: $key, value: $value)")
155+
156+
if(key.isEmpty()) {
157+
throw Exception("Cannot add tag with empty key")
158+
}
159+
110160
_propertiesModel.tags[key] = value
111161
}
112162

113163
override fun addTags(tags: Map<String, String>) {
114164
Logging.log(LogLevel.DEBUG, "setTags(tags: $tags)")
115165

166+
tags.forEach {
167+
if(it.key.isEmpty()) {
168+
throw Exception("Cannot add tag with empty key")
169+
}
170+
}
171+
116172
tags.forEach {
117173
_propertiesModel.tags[it.key] = it.value
118174
}
119175
}
120176

121177
override fun removeTag(key: String) {
122178
Logging.log(LogLevel.DEBUG, "removeTag(key: $key)")
179+
180+
if(key.isEmpty()) {
181+
throw Exception("Cannot remove tag with empty key")
182+
}
183+
123184
_propertiesModel.tags.remove(key)
124185
}
125186

126187
override fun removeTags(keys: Collection<String>) {
127188
Logging.log(LogLevel.DEBUG, "removeTags(keys: $keys)")
128189

190+
keys.forEach {
191+
if(it.isEmpty()) {
192+
throw Exception("Cannot remove tag with empty key")
193+
}
194+
}
195+
129196
keys.forEach {
130197
_propertiesModel.tags.remove(it)
131198
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/LoginUserFromSubscriptionOperation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class LoginUserFromSubscriptionOperation() : Operation(LoginUserFromSubscription
3535
override val groupComparisonType: GroupComparisonType = GroupComparisonType.NONE
3636
override val canStartExecute: Boolean = true
3737

38-
constructor(appId: String, onesignalId: String, playerId: String) : this() {
38+
constructor(appId: String, onesignalId: String, subscriptionId: String) : this() {
3939
this.appId = appId
4040
this.onesignalId = onesignalId
41-
this.subscriptionId = playerId
41+
this.subscriptionId = subscriptionId
4242
}
4343
}

0 commit comments

Comments
 (0)