-- MySQL Script generated by MySQL Workbench
-- Mon 20 Nov 2017 08:36:18 PM MST
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema accounthub
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema accounthub
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `accounthub` DEFAULT CHARACTER SET utf8 ;
USE `accounthub` ;

-- -----------------------------------------------------
-- Table `accounthub`.`acctstatus`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`acctstatus` (
  `statusid` INT NOT NULL AUTO_INCREMENT,
  `statuscode` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`statusid`),
  UNIQUE INDEX `statusid_UNIQUE` (`statusid` ASC),
  UNIQUE INDEX `statuscode_UNIQUE` (`statuscode` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`accttypes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`accttypes` (
  `typeid` INT NOT NULL AUTO_INCREMENT,
  `typecode` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`typeid`),
  UNIQUE INDEX `typeid_UNIQUE` (`typeid` ASC),
  UNIQUE INDEX `typecode_UNIQUE` (`typecode` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`accounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`accounts` (
  `uid` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(190) NOT NULL,
  `password` VARCHAR(255) NULL,
  `realname` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) NULL DEFAULT 'NOEMAIL@EXAMPLE.COM',
  `authsecret` VARCHAR(100) NULL,
  `phone1` VARCHAR(45) NOT NULL,
  `phone2` VARCHAR(45) NOT NULL,
  `acctstatus` INT NOT NULL DEFAULT 0,
  `accttype` INT NOT NULL,
  `deleted` TINYINT(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`uid`, `username`),
  UNIQUE INDEX `userid_UNIQUE` (`uid` ASC),
  UNIQUE INDEX `username_UNIQUE` (`username` ASC),
  INDEX `fk_accounts_acctstatus_idx` (`acctstatus` ASC),
  INDEX `email_index` (`email` ASC),
  INDEX `fk_accounts_accttypes1_idx` (`accttype` ASC),
  CONSTRAINT `fk_accounts_acctstatus`
    FOREIGN KEY (`acctstatus`)
    REFERENCES `accounthub`.`acctstatus` (`statusid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_accounts_accttypes1`
    FOREIGN KEY (`accttype`)
    REFERENCES `accounthub`.`accttypes` (`typeid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`apps`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`apps` (
  `appid` INT NOT NULL AUTO_INCREMENT,
  `appname` VARCHAR(45) NULL,
  `appcode` VARCHAR(45) NULL,
  PRIMARY KEY (`appid`),
  UNIQUE INDEX `appid_UNIQUE` (`appid` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`available_apps`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`available_apps` (
  `appid` INT NOT NULL,
  `uid` INT NOT NULL,
  PRIMARY KEY (`appid`, `uid`),
  INDEX `fk_apps_has_accounts_accounts1_idx` (`uid` ASC),
  INDEX `fk_apps_has_accounts_apps1_idx` (`appid` ASC),
  CONSTRAINT `fk_apps_has_accounts_apps1`
    FOREIGN KEY (`appid`)
    REFERENCES `accounthub`.`apps` (`appid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_apps_has_accounts_accounts1`
    FOREIGN KEY (`uid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`apikeys`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`apikeys` (
  `key` VARCHAR(60) NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY (`key`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`groups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`groups` (
  `groupid` INT NOT NULL,
  `groupname` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`groupid`),
  UNIQUE INDEX `groupid_UNIQUE` (`groupid` ASC),
  UNIQUE INDEX `groupname_UNIQUE` (`groupname` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`assigned_groups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`assigned_groups` (
  `groupid` INT NOT NULL,
  `uid` INT NOT NULL,
  PRIMARY KEY (`groupid`, `uid`),
  INDEX `fk_groups_has_accounts_accounts1_idx` (`uid` ASC),
  INDEX `fk_groups_has_accounts_groups1_idx` (`groupid` ASC),
  CONSTRAINT `fk_groups_has_accounts_groups1`
    FOREIGN KEY (`groupid`)
    REFERENCES `accounthub`.`groups` (`groupid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_groups_has_accounts_accounts1`
    FOREIGN KEY (`uid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`managers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`managers` (
  `managerid` INT NOT NULL,
  `employeeid` INT NOT NULL,
  PRIMARY KEY (`managerid`, `employeeid`),
  INDEX `fk_managers_accounts2_idx` (`employeeid` ASC),
  CONSTRAINT `fk_managers_accounts1`
    FOREIGN KEY (`managerid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_managers_accounts2`
    FOREIGN KEY (`employeeid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`logtypes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`logtypes` (
  `logtype` INT NOT NULL,
  `typename` VARCHAR(45) NULL,
  PRIMARY KEY (`logtype`),
  UNIQUE INDEX `logtype_UNIQUE` (`logtype` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`authlog`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`authlog` (
  `logid` INT NOT NULL AUTO_INCREMENT,
  `logtime` DATETIME NOT NULL,
  `logtype` INT NOT NULL,
  `uid` INT NULL,
  `ip` VARCHAR(45) NULL,
  `otherdata` VARCHAR(255) NULL,
  PRIMARY KEY (`logid`),
  UNIQUE INDEX `logid_UNIQUE` (`logid` ASC),
  INDEX `fk_authlog_logtypes1_idx` (`logtype` ASC),
  INDEX `fk_authlog_accounts1_idx` (`uid` ASC),
  CONSTRAINT `fk_authlog_logtypes1`
    FOREIGN KEY (`logtype`)
    REFERENCES `accounthub`.`logtypes` (`logtype`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_authlog_accounts1`
    FOREIGN KEY (`uid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`permissions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`permissions` (
  `permid` INT NOT NULL AUTO_INCREMENT,
  `permcode` VARCHAR(45) NOT NULL,
  `perminfo` VARCHAR(200) NULL,
  PRIMARY KEY (`permid`),
  UNIQUE INDEX `permid_UNIQUE` (`permid` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`assigned_permissions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`assigned_permissions` (
  `uid` INT NOT NULL,
  `permid` INT NOT NULL,
  PRIMARY KEY (`uid`, `permid`),
  INDEX `fk_permissions_has_accounts_accounts1_idx` (`uid` ASC),
  INDEX `fk_permissions_has_accounts_permissions1_idx` (`permid` ASC),
  CONSTRAINT `fk_permissions_has_accounts_permissions1`
    FOREIGN KEY (`permid`)
    REFERENCES `accounthub`.`permissions` (`permid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_permissions_has_accounts_accounts1`
    FOREIGN KEY (`uid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`mobile_codes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`mobile_codes` (
  `codeid` INT NOT NULL AUTO_INCREMENT,
  `uid` INT NOT NULL,
  `code` VARCHAR(45) NULL,
  PRIMARY KEY (`codeid`),
  UNIQUE INDEX `codeid_UNIQUE` (`codeid` ASC),
  INDEX `fk_mobile_codes_accounts1_idx` (`uid` ASC),
  UNIQUE INDEX `code_UNIQUE` (`code` ASC),
  CONSTRAINT `fk_mobile_codes_accounts1`
    FOREIGN KEY (`uid`)
    REFERENCES `accounthub`.`accounts` (`uid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accounthub`.`rate_limit`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `accounthub`.`rate_limit` (
  `ipaddr` VARCHAR(45) NOT NULL,
  `lastaction` DATETIME NULL,
  PRIMARY KEY (`ipaddr`))
ENGINE = MEMORY;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

-- -----------------------------------------------------
-- Data for table `accounthub`.`acctstatus`
-- -----------------------------------------------------
START TRANSACTION;
USE `accounthub`;
INSERT INTO `accounthub`.`acctstatus` (`statusid`, `statuscode`) VALUES (1, 'NORMAL');
INSERT INTO `accounthub`.`acctstatus` (`statusid`, `statuscode`) VALUES (2, 'LOCKED_OR_DISABLED');
INSERT INTO `accounthub`.`acctstatus` (`statusid`, `statuscode`) VALUES (3, 'CHANGE_PASSWORD');
INSERT INTO `accounthub`.`acctstatus` (`statusid`, `statuscode`) VALUES (4, 'TERMINATED');
INSERT INTO `accounthub`.`acctstatus` (`statusid`, `statuscode`) VALUES (5, 'ALERT_ON_ACCESS');

COMMIT;


-- -----------------------------------------------------
-- Data for table `accounthub`.`accttypes`
-- -----------------------------------------------------
START TRANSACTION;
USE `accounthub`;
INSERT INTO `accounthub`.`accttypes` (`typeid`, `typecode`) VALUES (1, 'LOCAL');
INSERT INTO `accounthub`.`accttypes` (`typeid`, `typecode`) VALUES (2, 'LDAP');
INSERT INTO `accounthub`.`accttypes` (`typeid`, `typecode`) VALUES (3, 'LIGHT');

COMMIT;


-- -----------------------------------------------------
-- Data for table `accounthub`.`logtypes`
-- -----------------------------------------------------
START TRANSACTION;
USE `accounthub`;
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (1, 'PORTAL_LOGIN_OK');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (2, 'PORTAL_LOGIN_FAILED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (3, 'PASSWORD_CHANGED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (4, 'API_LOGIN_OK');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (5, 'API_LOGIN_FAILED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (6, 'PORTAL_BAD_AUTHCODE');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (7, 'API_BAD_AUTHCODE');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (8, 'BAD_CAPTCHA');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (9, '2FA_ADDED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (10, '2FA_REMOVED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (11, 'PORTAL_LOGOUT');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (12, 'API_AUTH_OK');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (13, 'API_AUTH_FAILED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (14, 'API_BAD_KEY');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (15, 'LOG_CLEARED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (16, 'USER_REMOVED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (17, 'USER_ADDED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (18, 'USER_EDITED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (19, 'MOBILE_LOGIN_OK');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (20, 'MOBILE_LOGIN_FAILED');
INSERT INTO `accounthub`.`logtypes` (`logtype`, `typename`) VALUES (21, 'MOBILE_BAD_KEY');

COMMIT;


-- -----------------------------------------------------
-- Data for table `accounthub`.`permissions`
-- -----------------------------------------------------
START TRANSACTION;
USE `accounthub`;
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (1, 'ADMIN', 'System administrator');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (100, 'INV_VIEW', 'Access inventory system');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (101, 'INV_EDIT', 'Edit inventory system');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (200, 'TASKFLOOR', 'Access TaskFloor');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (300, 'QWIKCLOCK', 'Access QwikClock and punch in/out');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (301, 'QWIKCLOCK_MANAGE', 'Edit punches and other data for managed users');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (302, 'QWIKCLOCK_EDITSELF', 'Edit own punches and other data');
INSERT INTO `accounthub`.`permissions` (`permid`, `permcode`, `perminfo`) VALUES (303, 'QWIKCLOCK_ADMIN', 'Add and edit shifts and other data for all users');

COMMIT;

