From d96092ae438e4d57b3c99405e7a704af5b83f605 Mon Sep 17 00:00:00 2001 From: or-else Date: Fri, 7 Jun 2019 15:40:58 -0700 Subject: [PATCH] deleting finished, needs testing --- docker/README.md | 1 + docker/tinode/Dockerfile | 3 ++- docker/tinode/entrypoint.sh | 7 ++++++- server/main.go | 2 +- server/topic.go | 9 +++++++-- server/user.go | 4 ++-- server/validate/email/validate.go | 4 ++-- server/validate/tel/validate.go | 5 +++++ tinode-db/main.go | 3 ++- 9 files changed, 28 insertions(+), 10 deletions(-) diff --git a/docker/README.md b/docker/README.md index 799a5eca5..bacf3ceab 100644 --- a/docker/README.md +++ b/docker/README.md @@ -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). | diff --git a/docker/tinode/Dockerfile b/docker/tinode/Dockerfile index cfa54c77a..9a1c98ba3 100644 --- a/docker/tinode/Dockerfile +++ b/docker/tinode/Dockerfile @@ -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' diff --git a/docker/tinode/entrypoint.sh b/docker/tinode/entrypoint.sh index 78a700a74..0f041b32c 100644 --- a/docker/tinode/entrypoint.sh +++ b/docker/tinode/entrypoint.sh @@ -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 @@ -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. diff --git a/server/main.go b/server/main.go index 05aefd0fa..7974d5945 100644 --- a/server/main.go +++ b/server/main.go @@ -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() diff --git a/server/topic.go b/server/topic.go index c668a4fbe..eb901ae16 100644 --- a/server/topic.go +++ b/server/topic.go @@ -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 { @@ -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 diff --git a/server/user.go b/server/user.go index 504a1462d..675cca868 100644 --- a/server/user.go +++ b/server/user.go @@ -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. @@ -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: diff --git a/server/validate/email/validate.go b/server/validate/email/validate.go index 500b189d4..f3801d150 100644 --- a/server/validate/email/validate.go +++ b/server/validate/email/validate.go @@ -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. @@ -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. diff --git a/server/validate/tel/validate.go b/server/validate/tel/validate.go index f441845f8..455e21180 100644 --- a/server/validate/tel/validate.go +++ b/server/validate/tel/validate.go @@ -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{}) } diff --git a/tinode-db/main.go b/tinode-db/main.go index 1ea080938..60cce9815 100644 --- a/tinode-db/main.go +++ b/tinode-db/main.go @@ -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) @@ -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) }