Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Removed RUID dependency in favour of noeq
Browse files Browse the repository at this point in the history
  • Loading branch information
Paddy Foran committed Dec 21, 2012
1 parent 126b36f commit 42639dd
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 189 deletions.
52 changes: 26 additions & 26 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/fzzbt/radix/redis"
"io/ioutil"
"net/http"
"secondbit.org/ruid"
"strconv"
"time"
)

type Account struct {
Added time.Time `json:"added,omitempty"`
ID ruid.RUID `json:"id,omitempty"`
ID uint64 `json:"id,omitempty"`
Provider string `json:"provider,omitempty"`
// Provided by the provider
ForeignID string `json:"foreign_id,omitempty"`
Expand All @@ -27,14 +27,14 @@ type Account struct {
Timezone string `json:"timezone,omitempty"`
Gender string `json:"gender,omitempty"`
// private info that is stored, never shared
UserID ruid.RUID `json:"-"`
UserID uint64 `json:"-"`
accessToken string
refreshToken string
expires time.Time
}

func (account *Account) IsEmpty() bool {
return account.ID.String() == ruid.RUID(0).String()
return account.ID == 0
}

type googleAccount struct {
Expand Down Expand Up @@ -126,12 +126,12 @@ func (r *RequestBundle) GetAccount(access, refresh string, expiration time.Time)
Timezone: googAccount.Timezone,
Locale: googAccount.Locale,
Gender: googAccount.Gender,
UserID: ruid.RUID(0),
UserID: 0,
accessToken: access,
refreshToken: refresh,
expires: expiration,
}
id, err := gen.Generate([]byte(googAccount.ID))
id, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
return Account{}, err
Expand Down Expand Up @@ -219,7 +219,7 @@ func (r *RequestBundle) getAccountByForeignID(foreign_id string) (Account, error
r.Log.Error(err.Error())
return Account{}, nil
}
id, err := ruid.RUIDFromString(account_id)
id, err := strconv.ParseUint(account_id, 10, 64)
if err != nil {
r.Log.Error(err.Error())
return Account{}, err
Expand All @@ -234,16 +234,16 @@ func (r *RequestBundle) getAccountByForeignID(foreign_id string) (Account, error
return account, nil
}

func (r *RequestBundle) GetAccountByID(id ruid.RUID) (Account, error) {
func (r *RequestBundle) GetAccountByID(id uint64) (Account, error) {
// start instrumentation
reply := r.Repo.client.Hgetall("accounts:" + id.String())
reply := r.Repo.client.Hgetall("accounts:" + strconv.FormatUint(id, 10))
// report the request to the repo to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return Account{}, reply.Err
}
if reply.Type == redis.ReplyNil {
r.Log.Warn("Account not found. ID: %s", id)
r.Log.Warn("Account not found. ID: %d", id)
return Account{}, nil
}
hash, err := reply.Hash()
Expand All @@ -261,7 +261,7 @@ func (r *RequestBundle) GetAccountByID(id ruid.RUID) (Account, error) {
r.Log.Error(err.Error())
return Account{}, err
}
user_id, err := ruid.RUIDFromString(hash["user_id"])
user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
return Account{}, err
}
Expand Down Expand Up @@ -336,8 +336,8 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
from["gender"] = old_account.Gender
}
if old_account.UserID != account.UserID {
changes["user_id"] = account.UserID.String()
from["user_id"] = old_account.UserID.String()
changes["user_id"] = account.UserID
from["user_id"] = old_account.UserID
}
if old_account.accessToken != account.accessToken {
changes["access_token"] = account.accessToken
Expand All @@ -351,20 +351,20 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
changes["expires"] = account.expires.Format(time.RFC3339)
from["expires"] = old_account.expires.Format(time.RFC3339)
}
reply := r.Repo.client.Hmset("accounts:"+account.ID.String(), changes)
reply := r.Repo.client.Hmset("accounts:"+strconv.FormatUint(account.ID, 10), changes)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}

reply = r.Repo.client.Sadd("users:"+account.UserID.String()+":accounts", account.ID.String())
reply = r.Repo.client.Sadd("users:"+strconv.FormatUint(account.UserID, 10)+":accounts", account.ID, 10)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
r.AuditMap("accounts:"+account.ID.String(), from, changes)
r.AuditMap("accounts:"+strconv.FormatUint(account.ID, 10), from, changes)
// add repo call to instrumentation
return nil
}
Expand All @@ -381,7 +381,7 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
"locale": account.Locale,
"timezone": account.Timezone,
"gender": account.Gender,
"user_id": account.UserID.String(),
"user_id": account.UserID,
"access_token": account.accessToken,
"refresh_token": account.refreshToken,
"expires": account.expires.Format(time.RFC3339),
Expand All @@ -405,25 +405,25 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
"expires": "",
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
mc.Hmset("accounts:"+account.ID.String(), changes)
mc.Hmset("oauth_foreign_ids_to_accounts", account.ForeignID, account.ID.String())
mc.Sadd("users:"+account.UserID.String()+":accounts", account.ID.String())
mc.Hmset("accounts:"+strconv.FormatUint(account.ID, 10), changes)
mc.Hmset("oauth_foreign_ids_to_accounts", account.ForeignID, strconv.FormatUint(account.ID, 10))
mc.Sadd("users:"+strconv.FormatUint(account.UserID, 10)+":accounts", account.ID)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
r.AuditMap("accounts:"+account.ID.String(), from, changes)
r.Audit("oauth_foreign_ids_to_accounts", account.ForeignID, "", account.ID.String())
r.AuditMap("accounts:"+strconv.FormatUint(account.ID, 10), from, changes)
r.Audit("oauth_foreign_ids_to_accounts", account.ForeignID, "", strconv.FormatUint(account.ID, 10))
// report the repo request to instrumentation
// stop instrumentation
return nil
}

func (r *RequestBundle) GetAccountsByUser(user User) ([]Account, error) {
// start instrumentation
reply := r.Repo.client.Smembers("users:" + user.ID.String() + ":accounts")
reply := r.Repo.client.Smembers("users:" + strconv.FormatUint(user.ID, 10) + ":accounts")
// report the repo request to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
Expand Down Expand Up @@ -469,12 +469,12 @@ func (r *RequestBundle) GetAccountsByUser(user User) ([]Account, error) {
r.Log.Error(err.Error())
continue
}
user_id, err := ruid.RUIDFromString(hash["user_id"])
user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
}
id, err := ruid.RUIDFromString(ids[pos])
id, err := strconv.ParseUint(ids[pos], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
Expand Down Expand Up @@ -544,7 +544,7 @@ func (r *RequestBundle) UpdateAccountData(account Account) (Account, error) {
return account, nil
}

func (r *RequestBundle) AssociateUserWithAccount(account Account, user ruid.RUID) error {
func (r *RequestBundle) AssociateUserWithAccount(account Account, user uint64) error {
// begin instrumentation
account.UserID = user
err := r.storeAccount(account, true)
Expand Down
20 changes: 10 additions & 10 deletions audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package twocloud

import (
"github.com/fzzbt/radix/redis"
"secondbit.org/ruid"
"strconv"
"time"
)

Expand All @@ -21,7 +21,7 @@ func (a *Auditor) Close() {
}

type Change struct {
ID ruid.RUID `json:"id"`
ID uint64 `json:"id"`
Key string `json:"key"`
Field string `json:"field"`
From interface{} `json:"from"`
Expand All @@ -31,10 +31,10 @@ type Change struct {
Timestamp time.Time `json:"timestamp"`
}

func (a *Auditor) Insert(key, ip string, user User, from, to map[string]interface{}) error {
func (a *Auditor) Insert(r *RequestBundle, key, ip string, user User, from, to map[string]interface{}) error {
changes := []Change{}
for k, v := range to {
id, err := gen.Generate([]byte(key))
id, err := r.GetID()
if err != nil {
return err
}
Expand All @@ -53,11 +53,11 @@ func (a *Auditor) Insert(key, ip string, user User, from, to map[string]interfac
reply := a.client.MultiCall(func(mc *redis.MultiCall) {
for _, change := range changes {
user_str := ""
if change.User.ID != ruid.RUID(0) {
user_str = change.User.ID.String()
if change.User.ID != 0 {
user_str = strconv.FormatUint(change.User.ID, 10)
}
mc.Hmset("audit:"+change.Key+":item:"+change.ID.String(), "from", change.From, "to", change.To, "field", change.Field, "timestamp", change.Timestamp.Format(time.RFC3339), "user", user_str)
mc.Lpush("audit:"+key, change.ID.String())
mc.Hmset("audit:"+change.Key+":item:"+strconv.FormatUint(change.ID, 10), "from", change.From, "to", change.To, "field", change.Field, "timestamp", change.Timestamp.Format(time.RFC3339), "user", user_str)
mc.Lpush("audit:"+key, change.ID)
}
})
return reply.Err
Expand All @@ -69,7 +69,7 @@ func (r *RequestBundle) Audit(key, field, fromstr, tostr string) {
from[field] = fromstr
to := map[string]interface{}{}
to[field] = tostr
err := r.Auditor.Insert(key, r.Request.RemoteAddr, r.AuthUser, from, to)
err := r.Auditor.Insert(r, key, r.Request.RemoteAddr, r.AuthUser, from, to)
if err != nil {
r.Log.Error(err.Error())
}
Expand All @@ -78,7 +78,7 @@ func (r *RequestBundle) Audit(key, field, fromstr, tostr string) {

func (r *RequestBundle) AuditMap(key string, from, to map[string]interface{}) {
if r.Auditor != nil {
err := r.Auditor.Insert(key, r.Request.RemoteAddr, r.AuthUser, from, to)
err := r.Auditor.Insert(r, key, r.Request.RemoteAddr, r.AuthUser, from, to)
if err != nil {
r.Log.Error(err.Error())
}
Expand Down
23 changes: 12 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
)

type Config struct {
UseSubscriptions bool `json:"subscriptions"`
MaintenanceMode bool `json:"maintenance"`
Database redis.Config `json:"db"`
AuditDatabase redis.Config `json:"audit_db"`
StatsDatabase redis.Config `json:"stats_db"`
OAuth OAuthClient `json:"oauth"`
TrialPeriod time.Duration `json:"trial_period"`
GracePeriod time.Duration `json:"grace_period"`
UseSubscriptions bool `json:"subscriptions"`
MaintenanceMode bool `json:"maintenance"`
Database redis.Config `json:"db"`
AuditDatabase redis.Config `json:"audit_db"`
InstrumentationDatabase redis.Config `json:"instrumentation_db"`
OAuth OAuthClient `json:"oauth"`
TrialPeriod time.Duration `json:"trial_period"`
GracePeriod time.Duration `json:"grace_period"`
IDGenerators map[string]string `json:"id_gen"`
}

type OAuthClient struct {
ClientID string
ClientSecret string
CallbackURL string
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
CallbackURL string `json:"callback"`
}
Loading

0 comments on commit 42639dd

Please sign in to comment.