Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Add SMS country picker for default code
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Sep 23, 2020
1 parent c0dca2a commit f4fb354
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/server/assets/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/open-iconic/1.1.1/font/css/open-iconic-bootstrap.min.css"
integrity="sha256-BJ/G+e+y7bQdrYkS2RBTyNfBHpA9IuGaPmf9htub5MQ=" crossorigin="anonymous">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.0/css/intlTelInput.css"
integrity="sha384-TXDx4BvGGuJDYIKlcgXfDntJ100A809RRLB4W72MhjXJzakfj3ptxy4zER5qsxZH" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.0/js/intlTelInput.js"
integrity="sha384-PlunRcEpe5pvbKHPtkn6b/Ed9D5+mVGYwhlkrmyPEhWAUDZJZZu42bS2fMdceJft" crossorigin="anonymous"></script>

<title>{{if .title}}{{.title}}{{else}}Diagnosis Verification Server{{end}}</title>

Expand Down
9 changes: 9 additions & 0 deletions cmd/server/assets/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ <h1>Create verification code</h1>
$formArea.removeClass('d-none');
});

let phone = document.querySelector('#phone');
let iti = window.intlTelInput(phone, {
nationalMode: true,
{{- if $currentRealm.SMSCountry }}
initialCountry: '{{$currentRealm.SMSCountry}}',
{{- end }}
utilsScript: 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.0/js/utils.js',
});

{{if $currentRealm.RequireDate}}
let $dates = $('input#test-date,input#symptom-date');
$dates.on('input', function() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/assets/realm.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h1>Realm settings</h1>
<div class="form-group row">
<label for="emailVerifiedMode" class="col-sm-3">Email verified:</label>
<div class="col-sm-9">
<select name="emailVerifiedMode" id="emailVerifiedMode" class="form-control">
<select name="emailVerifiedMode" id="emailVerifiedMode" class="form-control custom-select">
<option value="1" {{if eq $realm.EmailVerifiedMode.String "required"}}selected{{end}}>Required</option>
<option value="0" {{if eq $realm.EmailVerifiedMode.String "prompt"}}selected{{end}}>Prompt after login</option>
<option value="2" {{if eq $realm.EmailVerifiedMode.String "optional"}}selected{{end}}>Optional</option>
Expand All @@ -165,7 +165,7 @@ <h1>Realm settings</h1>
<div class="form-group row">
<label for="MFAMode" class="col-sm-3">Multi factor auth:</label>
<div class="col-sm-9">
<select name="MFAMode" id="MFAMode" class="form-control">
<select name="MFAMode" id="MFAMode" class="form-control custom-select">
<option value="1" {{if eq $realm.MFAMode.String "required"}}selected{{end}}>Required</option>
<option value="0" {{if eq $realm.MFAMode.String "prompt"}}selected{{end}}>Prompt after login</option>
<option value="2" {{if eq $realm.MFAMode.String "optional"}}selected{{end}}>Optional</option>
Expand Down
16 changes: 16 additions & 0 deletions cmd/server/assets/realmadmin/_form_sms.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{{$realm := .realm}}
{{$smsConfig := .smsConfig}}
{{$countries := .countries}}

<p class="mb-4">
These are the settings for configuring the <a
Expand Down Expand Up @@ -70,6 +71,21 @@
</div>
</div>

<div class="form-label-group">
<select name="sms_country" id="sms_country" class="form-control custom-select">
<option disabled selected hidden>SMS country</option>
{{range $name, $value := $countries}}
<option value="{{$value}}"{{if eq $realm.SMSCountry $value}} selected{{end}}>{{$name}}</option>
{{end}}
</select>
{{template "errorable" $realm.ErrorsFor "smsCountry"}}
<small class="form-text text-muted">
This is the default value for the country code to use for SMS numbers.
Case workers will still be able to select a default value, but you should
set this to your most common region.
</small>
</div>

<div class="mt-4">
<input type="submit" class="btn btn-primary btn-block" value="Update SMS settings" />
</div>
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/realmadmin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c *Controller) HandleSettings() http.Handler {

SMS bool `form:"sms"`
UseSystemSMSConfig bool `form:"use_system_sms_config"`
SMSCountry string `form:"sms_country"`
TwilioAccountSid string `form:"twilio_account_sid"`
TwilioAuthToken string `form:"twilio_auth_token"`
TwilioFromNumber string `form:"twilio_from_number"`
Expand Down Expand Up @@ -130,6 +131,7 @@ func (c *Controller) HandleSettings() http.Handler {
// SMS
if form.SMS {
realm.UseSystemSMSConfig = form.UseSystemSMSConfig
realm.SMSCountry = form.SMSCountry
}

// Security
Expand Down Expand Up @@ -277,6 +279,7 @@ func (c *Controller) renderSettings(ctx context.Context, w http.ResponseWriter,
m := controller.TemplateMapFromContext(ctx)
m["realm"] = realm
m["smsConfig"] = smsConfig
m["countries"] = database.Countries
m["testTypes"] = map[string]database.TestType{
"confirmed": database.TestTypeConfirmed,
"likely": database.TestTypeConfirmed | database.TestTypeLikely,
Expand Down
246 changes: 246 additions & 0 deletions pkg/database/countries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package database

var Countries = map[string]string{
"Afghanistan": "af",
"Aland Islands": "ax",
"Albania": "al",
"Algeria": "dz",
"American Samoa": "as",
"Andorra": "ad",
"Angola": "ao",
"Anguilla": "ai",
"Antigua and Barbuda": "ag",
"Argentina": "ar",
"Armenia": "am",
"Aruba": "aw",
"Australia": "au",
"Austria": "at",
"Azerbaijan": "az",
"Bahamas": "bs",
"Bahrain": "bh",
"Bangladesh": "bd",
"Barbados": "bb",
"Belarus": "by",
"Belgium": "be",
"Belize": "bz",
"Benin": "bj",
"Bermuda": "bm",
"Bhutan": "bt",
"Bolivia": "bo",
"Bosnia and Herzegovina": "ba",
"Botswana": "bw",
"Brazil": "br",
"British Indian Ocean Territory": "io",
"British Virgin Islands": "vg",
"Brunei": "bn",
"Bulgaria": "bg",
"Burkina Faso": "bf",
"Burundi": "bi",
"Cambodia": "kh",
"Cameroon": "cm",
"Canada": "ca",
"Cape Verde": "cv",
"Caribbean Netherlands": "bq",
"Cayman Islands": "ky",
"Central African Republic": "cf",
"Chad": "td",
"Chile": "cl",
"China": "cn",
"Christmas Island": "cx",
"Cocos Islands": "cc",
"Colombia": "co",
"Comoros": "km",
"Congo": "cd",
"Cook Islands": "ck",
"Costa Rica": "cr",
"Côte d’Ivoire": "ci",
"Croatia": "hr",
"Cuba": "cu",
"Curaçao": "cw",
"Cyprus": "cy",
"Czech Republic": "cz",
"Denmark": "dk",
"Djibouti": "dj",
"Dominica": "dm",
"Dominican Republic": "do",
"Ecuador": "ec",
"Egypt": "eg",
"El Salvador": "sv",
"Equatorial Guinea": "gq",
"Eritrea": "er",
"Estonia": "ee",
"Ethiopia": "et",
"Falkland Islands": "fk",
"Faroe Islands": "fo",
"Fiji": "fj",
"Finland": "fi",
"France": "fr",
"French Guiana": "gf",
"French Polynesia": "pf",
"Gabon": "ga",
"Gambia": "gm",
"Georgia": "ge",
"Germany": "de",
"Ghana": "gh",
"Gibraltar": "gi",
"Greece": "gr",
"Greenland": "gl",
"Grenada": "gd",
"Guadeloupe": "gp",
"Guam": "gu",
"Guatemala": "gt",
"Guernsey": "gg",
"Guinea-Bissau": "gw",
"Guinea": "gn",
"Guyana": "gy",
"Haiti": "ht",
"Honduras": "hn",
"Hong Kong": "hk",
"Hungary": "hu",
"Iceland": "is",
"India": "in",
"Indonesia": "id",
"Iran": "ir",
"Iraq": "iq",
"Ireland": "ie",
"Isle of Man": "im",
"Israel": "il",
"Italy": "it",
"Jamaica": "jm",
"Japan": "jp",
"Jersey": "je",
"Jordan": "jo",
"Kazakhstan": "kz",
"Kenya": "ke",
"Kiribati": "ki",
"Kosovo": "xk",
"Kuwait": "kw",
"Kyrgyzstan": "kg",
"Laos": "la",
"Latvia": "lv",
"Lebanon": "lb",
"Lesotho": "ls",
"Liberia": "lr",
"Libya": "ly",
"Liechtenstein": "li",
"Lithuania": "lt",
"Luxembourg": "lu",
"Macau": "mo",
"Macedonia": "mk",
"Madagascar": "mg",
"Malawi": "mw",
"Malaysia": "my",
"Maldives": "mv",
"Mali": "ml",
"Malta": "mt",
"Marshall Islands": "mh",
"Martinique": "mq",
"Mauritania": "mr",
"Mauritius": "mu",
"Mayotte": "yt",
"Mexico": "mx",
"Micronesia": "fm",
"Moldova": "md",
"Monaco": "mc",
"Mongolia": "mn",
"Montenegro": "me",
"Montserrat": "ms",
"Morocco": "ma",
"Mozambique": "mz",
"Myanmar": "mm",
"Namibia": "na",
"Nauru": "nr",
"Nepal": "np",
"Netherlands": "nl",
"New Caledonia": "nc",
"New Zealand": "nz",
"Nicaragua": "ni",
"Niger": "ne",
"Nigeria": "ng",
"Niue": "nu",
"Norfolk Island": "nf",
"North Korea": "kp",
"Northern Mariana Islands": "mp",
"Norway": "no",
"Oman": "om",
"Pakistan": "pk",
"Palau": "pw",
"Palestine": "ps",
"Panama": "pa",
"Papua New Guinea": "pg",
"Paraguay": "py",
"Peru": "pe",
"Philippines": "ph",
"Poland": "pl",
"Portugal": "pt",
"Puerto Rico": "pr",
"Qatar": "qa",
"Réunion": "re",
"Romania": "ro",
"Russia": "ru",
"Rwanda": "rw",
"Saint Barthélemy": "bl",
"Saint Helena": "sh",
"Saint Kitts and Nevis": "kn",
"Saint Lucia": "lc",
"Saint Martin": "mf",
"Saint Pierre and Miquelon": "pm",
"Saint Vincent and the Grenadines": "vc",
"Samoa": "ws",
"San Marino": "sm",
"São Tomé and Príncipe": "st",
"Saudi Arabia": "sa",
"Senegal": "sn",
"Serbia": "rs",
"Seychelles": "sc",
"Sierra Leone": "sl",
"Singapore": "sg",
"Sint Maarten": "sx",
"Slovakia": "sk",
"Slovenia": "si",
"Solomon Islands": "sb",
"Somalia": "so",
"South Africa": "za",
"South Korea": "kr",
"South Sudan": "ss",
"Spain": "es",
"Sri Lanka": "lk",
"Sudan": "sd",
"Suriname": "sr",
"Svalbard and Jan Mayen": "sj",
"Swaziland": "sz",
"Sweden": "se",
"Switzerland": "ch",
"Syria": "sy",
"Taiwan": "tw",
"Tajikistan": "tj",
"Tanzania": "tz",
"Thailand": "th",
"Timor-Leste": "tl",
"Togo": "tg",
"Tokelau": "tk",
"Tonga": "to",
"Trinidad and Tobago": "tt",
"Tunisia": "tn",
"Turkey": "tr",
"Turkmenistan": "tm",
"Turks and Caicos Islands": "tc",
"Tuvalu": "tv",
"U.S. Virgin Islands": "vi",
"Uganda": "ug",
"Ukraine": "ua",
"United Arab Emirates": "ae",
"United Kingdom": "gb",
"United States": "us",
"Uruguay": "uy",
"Uzbekistan": "uz",
"Vanuatu": "vu",
"Vatican City": "va",
"Venezuela": "ve",
"Vietnam": "vn",
"Wallis and Futuna": "wf",
"Western Sahara": "eh",
"Yemen": "ye",
"Zambia": "zm",
"Zimbabwe": "zw",
}
19 changes: 19 additions & 0 deletions pkg/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,25 @@ func (db *Database) getMigrations(ctx context.Context) *gormigrate.Gormigrate {
}
}

return nil
},
},
{
ID: "00053-AddRealmSMSCountry",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Realm{}).Error
},
Rollback: func(tx *gorm.DB) error {
sqls := []string{
`ALTER TABLE realms DROP COLUMN IF EXISTS sms_country`,
}

for _, sql := range sqls {
if err := tx.Exec(sql).Error; err != nil {
return err
}
}

return nil
},
},
Expand Down
Loading

0 comments on commit f4fb354

Please sign in to comment.