-- phpMyAdmin SQL Dump
-- version 5.2.3
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jan 06, 2026 at 06:23 PM
-- Server version: 12.1.2-MariaDB-log
-- PHP Version: 8.4.15

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `aether_dev`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `id_random` varchar(22) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'NULL',
  `account_id` int(11) DEFAULT NULL,
  `contact_id` int(11) DEFAULT NULL COMMENT 'no longer going to be used',
  `organization_id` int(11) DEFAULT NULL COMMENT 'no longer going to be used',
  `person_id` int(11) DEFAULT NULL COMMENT 'no longer going to be used',
  `username` varchar(100) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `email` varchar(254) NOT NULL,
  `email_verified` tinyint(1) NOT NULL DEFAULT 0,
  `password` varchar(100) DEFAULT NULL,
  `allow_auth_key` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'allow use of auth key to sign in',
  `auth_key` varchar(22) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'auth key for single use log in',
  `enable` tinyint(1) NOT NULL DEFAULT 0,
  `enable_from` datetime DEFAULT current_timestamp(),
  `enable_to` datetime DEFAULT NULL,
  `super` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Super user for the entire system. Can do everything.',
  `manager` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Manager for the entire system. Can do almost everything.',
  `administrator` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Administrator for a client account. Can do almost anything within that account.',
  `public` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'public or shared account',
  `verified` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Is this a verified user account?',
  `level` int(11) DEFAULT NULL COMMENT 'access level (1 to 5)',
  `status_id` int(11) DEFAULT NULL COMMENT 'offline, away, dnd, online',
  `password_set_on` timestamp NOT NULL DEFAULT current_timestamp(),
  `password_reset_token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `password_reset_expire_on` datetime DEFAULT NULL,
  `logged_in_on` timestamp NULL DEFAULT NULL,
  `last_activity_on` timestamp NULL DEFAULT NULL,
  `hide` tinyint(1) DEFAULT NULL,
  `priority` tinyint(1) DEFAULT NULL,
  `sort` int(11) DEFAULT NULL COMMENT 'used as access level',
  `group` varchar(100) DEFAULT NULL COMMENT 'used as access group',
  `notes` text DEFAULT NULL,
  `other_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`other_json`)),
  `created_on` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_on` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='The user account.';

--
-- Triggers `user`
--
DELIMITER $$
CREATE TRIGGER `after_insert_user_person_id` AFTER INSERT ON `user` FOR EACH ROW BEGIN
   IF (NEW.person_id IS NOT NULL)
   THEN
       UPDATE person SET person.user_id = NEW.id WHERE person.id = NEW.person_id AND person.user_id IS NULL;
   END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `after_update_user_person_id` AFTER UPDATE ON `user` FOR EACH ROW BEGIN
   IF (NEW.person_id IS NOT NULL AND OLD.person_id IS NULL)
   THEN
       UPDATE person SET person.user_id = NEW.id WHERE person.id = NEW.person_id AND person.user_id IS NULL;
   END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_insert_user` BEFORE INSERT ON `user` FOR EACH ROW BEGIN
    BEGIN
        IF (NEW.id_random IS NULL OR NEW.id_random = '' OR LENGTH(NEW.id_random) < 6)
        THEN
            /* SET NEW.id_random = gen_rand_pattern('3C-2N-2N-2N'); */
            /* SET NEW.id_random = gen_rand_pattern('4C-2N-2N-2N'); */
            SET NEW.id_random = gen_rand_str(11, 'URL');
        END IF;
    END;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_update_user` BEFORE UPDATE ON `user` FOR EACH ROW BEGIN
    BEGIN
        IF (NEW.id_random IS NULL OR NEW.id_random = '' OR LENGTH(NEW.id_random) < 6)
        THEN
            /* SET NEW.id_random = gen_rand_pattern('3C-2N-2N-2N'); */
            /* SET NEW.id_random = gen_rand_pattern('4C-2N-2N-2N'); */
            SET NEW.id_random = gen_rand_str(11, 'URL');
        END IF;
    END;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_update_user_email` BEFORE UPDATE ON `user` FOR EACH ROW BEGIN
   IF (NEW.username IS NULL OR NEW.username = '')
   THEN
       SET NEW.username = NEW.email;
       SET NEW.notes = CONCAT(COALESCE(`NEW`.`notes`,''), ';;sys::overide empty username');
   END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `update_password_set_on` BEFORE UPDATE ON `user` FOR EACH ROW IF NEW.password <> OLD.password THEN SET NEW.password_set_on = CURRENT_TIMESTAMP; END IF
$$
DELIMITER ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user`
--
ALTER TABLE `user`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `id_random` (`id_random`),
  ADD KEY `username` (`username`) USING BTREE,
  ADD KEY `account_id` (`account_id`),
  ADD KEY `email` (`email`),
  ADD KEY `email_2` (`email`),
  ADD KEY `created_on` (`created_on`),
  ADD KEY `updated_on` (`updated_on`),
  ADD KEY `enable` (`enable`),
  ADD KEY `name` (`name`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
