Skip to content

Commit

Permalink
sun, wax & wings. added voucher with params
Browse files Browse the repository at this point in the history
  • Loading branch information
edospadoni committed Mar 27, 2018
1 parent 3bd3150 commit 64bc3d0
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 47 deletions.
10 changes: 9 additions & 1 deletion deploy/ansible/roles/icarodb/files/icaro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ CREATE TABLE `hotspot_preferences` (
PRIMARY KEY(`id`)
);

AutoLogin bool `db:"auto_login" json:"auto_login"`
BandwidthUp int `db:"bandwidth_up" json:"bandwidth_up"`
BandwidthDown int `db:"bandwidth_down" json:"bandwidth_down"`
Duration time.Time `db:"duration" json:"duration"`

CREATE TABLE `hotspot_vouchers` (
`id` serial,
`hotspot_id` bigint unsigned NOT NULL,
`code` varchar(250) NOT NULL,
`expires` datetime NOT NULL,
`auto_login` tinyint NOT NULL,
`bandwidth_up` integer unsigned,
`bandwidth_down` integer unsigned,
`duration` integer unsigned,
FOREIGN KEY (`hotspot_id`) REFERENCES hotspots(`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
UNIQUE KEY (`hotspot_id`, `code`),
PRIMARY KEY(`id`)
Expand Down
8 changes: 5 additions & 3 deletions sun/sun-api/methods/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package methods
import (
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/mysql"
Expand All @@ -45,8 +44,11 @@ func CreateVoucher(c *gin.Context) {
}

hotspotVoucher := models.HotspotVoucher{
Code: json.Code,
Expires: time.Time{},
Code: json.Code,
AutoLogin: json.AutoLogin,
BandwidthUp: json.BandwidthUp,
BandwidthDown: json.BandwidthDown,
Duration: json.Duration,
}

hotspotVoucher.HotspotId = json.HotspotId
Expand Down
13 changes: 7 additions & 6 deletions sun/sun-api/models/hotspot_voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@

package models

import "time"

type HotspotVoucher struct {
Id int `db:"id" json:"id"`
HotspotId int `db:"hotspot_id" json:"hotspot_id"`
Code string `db:"code" json:"code"`
Expires time.Time `db:"expires" json:"expires"`
Id int `db:"id" json:"id"`
HotspotId int `db:"hotspot_id" json:"hotspot_id"`
Code string `db:"code" json:"code"`
AutoLogin bool `db:"auto_login" json:"auto_login"`
BandwidthUp int `db:"bandwidth_up" json:"bandwidth_up"`
BandwidthDown int `db:"bandwidth_down" json:"bandwidth_down"`
Duration int `db:"duration" json:"duration"`
}
88 changes: 70 additions & 18 deletions sun/sun-ui/src/components/details-view/HotspotsDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@
<td class="fancy">
<strong>{{ props.row.code }}</strong>
</td>
<td class="fancy">{{ props.row.expires | formatDate }}</td>
<td class="fancy"><span :class="['pficon', props.row.auto_login ? 'pficon-ok' : 'pficon-error-circle-o']"></span></td>
<td class="fancy">
<div>
<strong>{{ $t('user.kbps_down') }}</strong>: {{ props.row.bandwidth_down || '-' }}</div>
<div>
<strong>{{ $t('user.kbps_up') }}</strong>: {{ props.row.bandwidth_up || '-' }}</div>
</td>
<td class="fancy">{{ props.row.duration }} ({{$t('hotspot.days')}})</td>
<td>
<button v-on:click="printVoucher(props.row.code)" class="btn btn-primary" type="button">
<span class="fa fa-print"></span>
Expand Down Expand Up @@ -282,6 +289,31 @@
<input v-model="vouchersCount" type="text" id="textInput-modal-markup" class="form-control">
</div>
</div>

<div class="form-group">
<label class="col-sm-5 control-label" for="textInput-modal-markup">{{$t('hotspot.auto_login')}}</label>
<div class="col-sm-7">
<input v-model="newVoucher.auto_login" type="checkbox" id="textInput-modal-markup" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label" for="textInput-modal-markup">{{$t('hotspot.bandwidth_down')}}</label>
<div class="col-sm-7">
<input v-model="newVoucher.bandwidth_down" type="number" id="textInput-modal-markup" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label" for="textInput-modal-markup">{{$t('hotspot.bandwidth_up')}}</label>
<div class="col-sm-7">
<input v-model="newVoucher.bandwidth_up" type="number" id="textInput-modal-markup" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label" for="textInput-modal-markup">{{$t('hotspot.duration')}} ({{$t('hotspot.days')}})</label>
<div class="col-sm-7">
<input v-model="newVoucher.duration" type="number" id="textInput-modal-markup" class="form-control">
</div>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -296,7 +328,7 @@
</template>

<script>
import HotspotService from '../../services/hotspot';
import HotspotService from '../../services/hotspot';
import PreferenceService from '../../services/preference';
import AccountService from '../../services/account';
import UnitService from '../../services/unit';
Expand Down Expand Up @@ -355,6 +387,12 @@
data: []
},
vouchersCount: 1,
newVoucher: {
auto_login: true,
bandwidth_down: 0,
bandwidth_up: 0,
duration: 7
},
preferences: {
isLoading: true,
global: {},
Expand Down Expand Up @@ -388,8 +426,16 @@
filterable: false,
sortable: false,
}, {
label: this.$i18n.t('hotspot.expires'),
field: 'expires',
label: this.$i18n.t('hotspot.auto_login'),
field: 'auto_login',
filterable: false,
}, {
label: this.$i18n.t('hotspot.bandwidth_limit'),
field: 'bandwidth',
filterable: false,
}, {
label: this.$i18n.t('hotspot.duration'),
field: 'duration',
filterable: false,
},
{
Expand All @@ -401,36 +447,38 @@
tableLangsTexts: this.tableLangs(),
uploadLangstexts: this.uploadImageLangs(),
user: this.get("loggedUser"),
displayCaptivePortalOptions: this.get("loggedUser") && this.get("loggedUser").subscription && this.get("loggedUser").subscription.subscription_plan && this.get("loggedUser").subscription.subscription_plan.wings_customization || this.get("loggedUser").account_type == "admin",
displayCaptivePortalOptions: this.get("loggedUser") && this.get("loggedUser").subscription && this.get(
"loggedUser").subscription.subscription_plan && this.get("loggedUser").subscription.subscription_plan.wings_customization ||
this.get("loggedUser").account_type == "admin",
customToolbar: [
['bold', 'italic', 'underline'],
['image', 'code-block']
],
userLink:{
name:'Users',
params:{
userLink: {
name: 'Users',
params: {
hotspotId: this.$route.params.id
}
},
unitLink:{
unitLink: {
name: 'Units',
params: {
hotspotId: this.$route.params.id
}
},
sessionLink:{
sessionLink: {
name: 'Sessions',
params: {
hotspotId: this.$route.params.id
}
},
accountLink:{
accountLink: {
name: 'Accounts',
params: {
hotspotId: this.$route.params.id
}
},
deviceLink:{
deviceLink: {
name: 'Devices',
params: {
hotspotId: this.$route.params.id
Expand All @@ -455,6 +503,10 @@
context.hotspotCreateVoucher({
hotspot_id: parseInt(context.$route.params.id),
code: context.generateVoucher(),
auto_login: context.newVoucher.auto_login,
bandwidth_down: parseInt(context.newVoucher.bandwidth_down),
bandwidth_up: parseInt(context.newVoucher.bandwidth_up),
duration: parseInt(context.newVoucher.duration),
}, success => {
resolve()
}, error => {
Expand Down Expand Up @@ -523,7 +575,7 @@
console.log(error.body)
this.totals.users.isLoading = false
})
this.deviceGetAll(this.$route.params.id, success => {
this.deviceGetAll(this.$route.params.id, "", success => {
this.totals.devices.count = success.body.length
this.totals.devices.isLoading = false
}, error => {
Expand Down Expand Up @@ -686,10 +738,10 @@
</script>

<style scoped>
textarea {
width: 100%;
min-height: 180px;
resize: vertical;
}
textarea {
width: 100%;
min-height: 180px;
resize: vertical;
}
</style>
7 changes: 6 additions & 1 deletion sun/sun-ui/src/i18n/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"expires": "Expires",
"preferences_captive": "Preferences Captive",
"captive_preview": "Preview",
"days": "Days",
"user_expiration_days": "User expiration (days)",
"captive_1_redir": "Captive Portal redirect",
"captive_2_title": "Captive Portal title",
Expand All @@ -83,7 +84,11 @@
"voucher_creation_count": "Vouchers number",
"auto_login": "Auto login",
"CoovaChilli-Bandwidth-Max-Down": "Bandwidth Max Down (Kbps)",
"CoovaChilli-Bandwidth-Max-Up": "Bandwidth Max Up (Kbps)"
"CoovaChilli-Bandwidth-Max-Up": "Bandwidth Max Up (Kbps)",
"bandwidth_limit": "Bandwidth Limit",
"bandwidth_down": "Bandwidth Max Down (Kbps)",
"bandwidth_up": "Bandwidth Max Up (Kbps)",
"duration": "Duration"
},
"user": {
"username": "Username",
Expand Down
49 changes: 33 additions & 16 deletions wax/methods/auth_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func SMSAuth(c *gin.Context) {
uuid := c.Query("uuid")
sessionId := c.Query("sessionid")
reset := c.Query("reset")
voucherCode := c.Query("voucher_code")

if number == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "number is required"})
Expand Down Expand Up @@ -85,6 +86,21 @@ func SMSAuth(c *gin.Context) {
autoLogin := utils.GetHotspotPreferencesByKey(unit.HotspotId, "auto_login")
autoLoginBool, _ := strconv.ParseBool(autoLogin.Value)

// retrieve voucher
if len(voucherCode) > 0 {
voucher := utils.GetVoucherByCode(voucherCode, unit.HotspotId)

daysInt = voucher.Duration
downInt = voucher.BandwidthDown
upInt = voucher.BandwidthUp
autoLoginBool = voucher.AutoLogin

// delete voucher
db := database.Database()
db.Delete(&voucher)
db.Close()
}

newUser := models.User{
HotspotId: unit.HotspotId,
Name: number,
Expand Down Expand Up @@ -152,6 +168,7 @@ func EmailAuth(c *gin.Context) {
uuid := c.Query("uuid")
sessionId := c.Query("sessionid")
reset := c.Query("reset")
voucherCode := c.Query("voucher_code")

if email == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "email is required"})
Expand Down Expand Up @@ -188,6 +205,21 @@ func EmailAuth(c *gin.Context) {
autoLogin := utils.GetHotspotPreferencesByKey(unit.HotspotId, "auto_login")
autoLoginBool, _ := strconv.ParseBool(autoLogin.Value)

// retrieve voucher
if len(voucherCode) > 0 {
voucher := utils.GetVoucherByCode(voucherCode, unit.HotspotId)

daysInt = voucher.Duration
downInt = voucher.BandwidthDown
upInt = voucher.BandwidthUp
autoLoginBool = voucher.AutoLogin

// delete voucher
db := database.Database()
db.Delete(&voucher)
db.Close()
}

newUser := models.User{
HotspotId: unit.HotspotId,
Name: email,
Expand Down Expand Up @@ -264,22 +296,7 @@ func VoucherAuth(c *gin.Context) {
if voucher.Id == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Voucher is invalid"})
} else {
if !voucher.Expires.IsZero() && voucher.Expires.Before(time.Now().UTC()) {
c.JSON(http.StatusOK, gin.H{"message": "Voucher is expired"})
} else {
// read hotspot preferences
days := utils.GetHotspotPreferencesByKey(unit.HotspotId, "voucher_expiration_days")
daysInt, _ := strconv.Atoi(days.Value)

// update voucher expiration
voucher.Expires = time.Now().UTC().AddDate(0, 0, daysInt)

db := database.Database()
db.Save(&voucher)
db.Close()

c.JSON(http.StatusOK, gin.H{"message": "Voucher is valid"})
}
c.JSON(http.StatusOK, gin.H{"message": "Voucher is valid", "code": voucher.Code})
}

}
Loading

0 comments on commit 64bc3d0

Please sign in to comment.