Skip to content

Commit

Permalink
deleting finished, needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jun 7, 2019
1 parent 59a485b commit d96092a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ You can specify the following environment variables when issuing `docker run` co
| `MYSQL_DSN` | string | `'root@tcp(mysql)/tinode'` | MySQL [DSN](https://github.com/go-sql-driver/mysql#dsn-data-source-name). |
| `PLUGIN_PYTHON_CHAT_BOT_ENABLED` | bool | `false` | Enable calling into the plugin provided by Python chatbot |
| `RESET_DB` | bool | `false` | Drop and recreate the database. |
| `SAMPLE_DATA` | string | _see comment_ | File with sample data to load. Default `data.json` when resetting or generating new DB, none when upgrading. Use `-` to disable |
| `SMTP_DOMAINS` | string | | White list of email domains; when non-empty, accept registrations with emails from these domains only (email verification). |
| `SMTP_HOST_URL` | string | `'http://localhost:6060/'` | URL of the host where the webapp is running (email verification). |
| `SMTP_PASSWORD` | string | | Password to use for authentication with the SMTP server (email verification). |
Expand Down
3 changes: 2 additions & 1 deletion docker/tinode/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ ENV RESET_DB=false
ENV UPGRADE_DB=false

# Load sample data to database from data.json.
ENV LOAD_SAMPLE_DATA=data.json
ARG DEFAULT_SAMPLE_DATA=data.json
ENV SAMPLE_DATA=

# The MySQL DSN connection.
ENV MYSQL_DSN='root@tcp(mysql)/tinode'
Expand Down
7 changes: 6 additions & 1 deletion docker/tinode/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ else
done < config.template
fi

# Load default sample data when generating or resetting the database.
if [[ -z "$SAMPLE_DATA" && "$UPGRADE_DB" = "false" ]] ; then
SAMPLE_DATA="$DEFAULT_SAMPLE_DATA"
fi

# If push notifications are enabled, generate client-side firebase config file.
if [ ! -z "$FCM_PUSH_ENABLED" ] ; then
# Write client config to static/firebase-init.js
Expand All @@ -51,7 +56,7 @@ else
fi

# Initialize the database if it has not been initialized yet or if data reset/upgrade has been requested.
./init-db --reset=${RESET_DB} --upgrade=${UPGRADE_DB} --config=${CONFIG} --data=data.json | grep "usr;tino;" > /botdata/tino-password
./init-db --reset=${RESET_DB} --upgrade=${UPGRADE_DB} --config=${CONFIG} --data=${SAMPLE_DATA} | grep "usr;tino;" > /botdata/tino-password

if [ -s /botdata/tino-password ] ; then
# Convert Tino's authentication credentials into a cookie file.
Expand Down
2 changes: 1 addition & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func main() {

err := store.Open(workerId, string(config.Store))
if err != nil {
log.Fatal("Failed to connect to DB:", err)
log.Fatal("Failed to connect to DB: ", err)
}
defer func() {
store.Close()
Expand Down
9 changes: 7 additions & 2 deletions server/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func (t *Topic) run(hub *Hub) {
case constMsgDelTopic:
err = t.replyDelTopic(hub, meta.sess, asUid, meta.pkt.Del)
case constMsgDelCred:
err = t.replyDelCred(hub, meta.sess, asUid, meta.pkt.Del)
err = t.replyDelCred(hub, meta.sess, asUid, authLevel, meta.pkt.Del)
}

if err != nil {
Expand Down Expand Up @@ -2143,12 +2143,17 @@ func (t *Topic) replyDelTopic(h *Hub, sess *Session, asUid types.Uid, del *MsgCl
}

// Delete credential
func (t *Topic) replyDelCred(h *Hub, sess *Session, asUid types.Uid, del *MsgClientDel) error {
func (t *Topic) replyDelCred(h *Hub, sess *Session, asUid types.Uid, authLvl auth.Level, del *MsgClientDel) error {
now := types.TimeNow()

if t.cat != types.TopicCatMe {
sess.queueOut(ErrPermissionDenied(del.Id, t.original(asUid), now))
return errors.New("del.cred: invalid topic category")
}

err := deleteCred(asUid, authLvl, del.Cred)
sess.queueOut(decodeStoreError(err, del.Id, del.Topic, now, nil))
return err
}

// Delete subscription
Expand Down
4 changes: 2 additions & 2 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func deleteCred(uid types.Uid, authLvl auth.Level, cred *MsgCredClient) error {
// Get validated credentials.
alreadyValidatedCreds, err := store.Users.GetAllCreds(uid, true)
if err != nil {
return nil, err
return err
}

// Index credential methods.
Expand All @@ -436,7 +436,7 @@ func deleteCred(uid types.Uid, authLvl auth.Level, cred *MsgCredClient) error {
}

// The credential is either not required or more than one credential is validated for the given method.
return vld.Remove(uid, value)
return vld.Remove(uid, cred.Value)
}

// Request to delete a user:
Expand Down
4 changes: 2 additions & 2 deletions server/validate/email/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (v *validator) Check(user t.Uid, resp string) (string, error) {
if resp == t.NullValue {
// The user is asking to clear validation status effectively removing the credential.
// Unvalidated credential cannot be removed to prevent abuse of going above MaxRetries.
return "", store.Users.DelCred(user, "email")
return "", store.Users.DelCred(user, "email", cred.Value)
}

// The credential has been already validated. No change.
Expand All @@ -263,7 +263,7 @@ func (v *validator) Check(user t.Uid, resp string) (string, error) {

// Delete deletes user's records.
func (v *validator) Delete(user t.Uid) error {
return store.Users.DelCred(user, "email")
return store.Users.DelCred(user, "email", "")
}

// Remove deactivates or removes user's credential.
Expand Down
5 changes: 5 additions & 0 deletions server/validate/tel/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (*validator) Delete(user t.Uid) error {
return nil
}

// Remove or disable the given record
func (*validator) Remove(user t.Uid, value string) error {
return nil
}

func init() {
store.RegisterValidator("tel", &validator{})
}
3 changes: 2 additions & 1 deletion tinode-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func main() {
flag.Parse()

var data Data
if *datafile != "" {
if *datafile != "" && *data != "-" {
raw, err := ioutil.ReadFile(*datafile)
if err != nil {
log.Fatal("Failed to parse data:", err)
Expand Down Expand Up @@ -224,6 +224,7 @@ func main() {
// Reset or create DB
err = store.InitDb(config, true)
}

if err != nil {
log.Fatal("Failed to init DB:", err)
}
Expand Down

0 comments on commit d96092a

Please sign in to comment.