
jbe@440: BEGIN;
jbe@440: 
jbe@440: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@440:   SELECT * FROM (VALUES ('3.0.4', 3, 0, 4))
jbe@440:   AS "subquery"("string", "major", "minor", "revision");
jbe@440: 
jbe@440: ALTER TABLE "member" ADD COLUMN "authority"       TEXT;
jbe@440: ALTER TABLE "member" ADD COLUMN "authority_uid"   TEXT;
jbe@440: ALTER TABLE "member" ADD COLUMN "authority_login" TEXT;
jbe@440: 
jbe@440: COMMENT ON COLUMN "member"."authority"       IS 'NULL if LiquidFeedback Core is authoritative for the member account; otherwise a string that indicates the source/authority of the external account (e.g. ''LDAP'' for an LDAP account)';
jbe@440: COMMENT ON COLUMN "member"."authority_uid"   IS 'Unique identifier (unique per "authority") that allows to identify an external account (e.g. even if the login name changes)';
jbe@440: COMMENT ON COLUMN "member"."authority_login" IS 'Login name for external accounts (field is not unique!)';
jbe@440: 
jbe@440: ALTER TABLE "member" ADD CONSTRAINT "authority_requires_uid_and_vice_versa" 
jbe@440:   CHECK ("authority" NOTNULL = "authority_uid" NOTNULL);
jbe@440: 
jbe@440: ALTER TABLE "member" ADD CONSTRAINT "authority_uid_unique_per_authority"
jbe@440:   UNIQUE ("authority", "authority_uid");
jbe@440: 
jbe@440: ALTER TABLE "member" ADD CONSTRAINT "authority_login_requires_authority"
jbe@440:   CHECK ("authority" NOTNULL OR "authority_login" ISNULL);
jbe@440: 
jbe@440: CREATE INDEX "member_authority_login_idx" ON "member" ("authority_login");
jbe@440: 
jbe@440: ALTER TABLE "session" ADD COLUMN "authority"       TEXT;
jbe@440: ALTER TABLE "session" ADD COLUMN "authority_uid"   TEXT; 
jbe@440: ALTER TABLE "session" ADD COLUMN "authority_login" TEXT;
jbe@440: 
jbe@440: COMMENT ON COLUMN "session"."authority"         IS 'Temporary store for "member"."authority" during member account creation';
jbe@440: COMMENT ON COLUMN "session"."authority_uid"     IS 'Temporary store for "member"."authority_uid" during member account creation';
jbe@440: COMMENT ON COLUMN "session"."authority_login"   IS 'Temporary store for "member"."authority_login" during member account creation';
jbe@440: 
jbe@441: CREATE OR REPLACE FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE)
jbe@441:   RETURNS VOID
jbe@441:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@441:     BEGIN
jbe@441:       UPDATE "member" SET
jbe@441:         "last_login"                   = NULL,
jbe@441:         "last_delegation_check"        = NULL,
jbe@441:         "login"                        = NULL,
jbe@441:         "password"                     = NULL,
jbe@441:         "authority"                    = NULL,
jbe@441:         "authority_uid"                = NULL,
jbe@441:         "authority_login"              = NULL,
jbe@441:         "locked"                       = TRUE,
jbe@441:         "active"                       = FALSE,
jbe@441:         "notify_email"                 = NULL,
jbe@441:         "notify_email_unconfirmed"     = NULL,
jbe@441:         "notify_email_secret"          = NULL,
jbe@441:         "notify_email_secret_expiry"   = NULL,
jbe@441:         "notify_email_lock_expiry"     = NULL,
jbe@441:         "login_recovery_expiry"        = NULL,
jbe@441:         "password_reset_secret"        = NULL,
jbe@441:         "password_reset_secret_expiry" = NULL,
jbe@441:         "organizational_unit"          = NULL,
jbe@441:         "internal_posts"               = NULL,
jbe@441:         "realname"                     = NULL,
jbe@441:         "birthday"                     = NULL,
jbe@441:         "address"                      = NULL,
jbe@441:         "email"                        = NULL,
jbe@441:         "xmpp_address"                 = NULL,
jbe@441:         "website"                      = NULL,
jbe@441:         "phone"                        = NULL,
jbe@441:         "mobile_phone"                 = NULL,
jbe@441:         "profession"                   = NULL,
jbe@441:         "external_memberships"         = NULL,
jbe@441:         "external_posts"               = NULL,
jbe@441:         "statement"                    = NULL
jbe@441:         WHERE "id" = "member_id_p";
jbe@441:       -- "text_search_data" is updated by triggers
jbe@441:       DELETE FROM "setting"            WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "setting_map"        WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "member_image"       WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "contact"            WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "ignored_member"     WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "session"            WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "area_setting"       WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "issue_setting"      WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "ignored_initiative" WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "membership"         WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "delegation"         WHERE "truster_id" = "member_id_p";
jbe@441:       DELETE FROM "non_voter"          WHERE "member_id" = "member_id_p";
jbe@441:       DELETE FROM "direct_voter" USING "issue"
jbe@441:         WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@441:         AND "issue"."closed" ISNULL
jbe@441:         AND "member_id" = "member_id_p";
jbe@441:       RETURN;
jbe@441:     END;
jbe@441:   $$;
jbe@441: 
jbe@441: CREATE OR REPLACE FUNCTION "delete_private_data"()
jbe@441:   RETURNS VOID
jbe@441:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@441:     BEGIN
jbe@441:       DELETE FROM "temporary_transaction_data";
jbe@441:       DELETE FROM "member" WHERE "activated" ISNULL;
jbe@441:       UPDATE "member" SET
jbe@441:         "invite_code"                  = NULL,
jbe@441:         "invite_code_expiry"           = NULL,
jbe@441:         "admin_comment"                = NULL,
jbe@441:         "last_login"                   = NULL,
jbe@441:         "last_delegation_check"        = NULL,
jbe@441:         "login"                        = NULL,
jbe@441:         "password"                     = NULL,
jbe@441:         "authority"                    = NULL,
jbe@441:         "authority_uid"                = NULL,
jbe@441:         "authority_login"              = NULL,
jbe@441:         "lang"                         = NULL,
jbe@441:         "notify_email"                 = NULL,
jbe@441:         "notify_email_unconfirmed"     = NULL,
jbe@441:         "notify_email_secret"          = NULL,
jbe@441:         "notify_email_secret_expiry"   = NULL,
jbe@441:         "notify_email_lock_expiry"     = NULL,
jbe@441:         "notify_level"                 = NULL,
jbe@441:         "login_recovery_expiry"        = NULL,
jbe@441:         "password_reset_secret"        = NULL,
jbe@441:         "password_reset_secret_expiry" = NULL,
jbe@441:         "organizational_unit"          = NULL,
jbe@441:         "internal_posts"               = NULL,
jbe@441:         "realname"                     = NULL,
jbe@441:         "birthday"                     = NULL,
jbe@441:         "address"                      = NULL,
jbe@441:         "email"                        = NULL,
jbe@441:         "xmpp_address"                 = NULL,
jbe@441:         "website"                      = NULL,
jbe@441:         "phone"                        = NULL,
jbe@441:         "mobile_phone"                 = NULL,
jbe@441:         "profession"                   = NULL,
jbe@441:         "external_memberships"         = NULL,
jbe@441:         "external_posts"               = NULL,
jbe@441:         "formatting_engine"            = NULL,
jbe@441:         "statement"                    = NULL;
jbe@441:       -- "text_search_data" is updated by triggers
jbe@441:       DELETE FROM "setting";
jbe@441:       DELETE FROM "setting_map";
jbe@441:       DELETE FROM "member_relation_setting";
jbe@441:       DELETE FROM "member_image";
jbe@441:       DELETE FROM "contact";
jbe@441:       DELETE FROM "ignored_member";
jbe@441:       DELETE FROM "session";
jbe@441:       DELETE FROM "area_setting";
jbe@441:       DELETE FROM "issue_setting";
jbe@441:       DELETE FROM "ignored_initiative";
jbe@441:       DELETE FROM "initiative_setting";
jbe@441:       DELETE FROM "suggestion_setting";
jbe@441:       DELETE FROM "non_voter";
jbe@441:       DELETE FROM "direct_voter" USING "issue"
jbe@441:         WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@441:         AND "issue"."closed" ISNULL;
jbe@441:       RETURN;
jbe@441:     END;
jbe@441:   $$;
jbe@441: 
jbe@440: COMMIT;




