#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = Lyon

"""
##############
##   用户   ##
##############

##############
##   角色   ##
##############

##############
##   权限   ##
##############

##############
##   菜单   ##
##############
"""

import hashlib

from lib.token import jwt_decode, jwt_encode
from base.db.mysql import MySQLModel, Func
from peewee import IntegerField, CharField, SmallIntegerField, FloatField


class RbacUser(MySQLModel):
    """用户表"""

    class Meta:
        table_name = 'rbac_user'

    USING_NO = 0
    USING_IS = 1

    nickname = CharField(max_length=32, verbose_name='昵称')
    phone = CharField(max_length=11, verbose_name='手机号')
    password = CharField(max_length=64, verbose_name='密码')
    email = CharField(max_length=64, verbose_name='邮箱')
    is_using = SmallIntegerField(default=USING_IS, verbose_name='是否禁用')

    def encode_token(self):
        from base import app
        data = {
            'id': self.id
        }
        return jwt_encode(data, app.config.SECRET_KEY)

    @classmethod
    def decode_token(cls, token):
        from base import app
        return jwt_decode(token, app.config.SECRET_KEY).get('data')


class RbacRole(MySQLModel):
    """角色表"""

    class Meta:
        table_name = 'rbac_role'

    name = CharField(max_length=32, verbose_name='角色名称')


class RbacUserRole(MySQLModel):
    """用户拥有的角色关系表"""

    class Meta:
        table_name = 'rbac_user_role'

    user_id = IntegerField(verbose_name='用户id')
    role_id = IntegerField(verbose_name='角色id')


class RbacPermission(MySQLModel):
    """权限表"""

    class Meta:
        table_name = 'rbac_permission'

    name = CharField(max_length=32, verbose_name='权限名称')
    alias = CharField(max_length=64, verbose_name='权限别名')


class RbacRolePermission(MySQLModel):
    """角色权限关系表"""

    class Meta:
        table_name = 'rbac_role_permission'

    role_id = IntegerField(verbose_name='角色id')
    permission_id = IntegerField(verbose_name='权限id')


class RbacMenu(MySQLModel):
    """菜单表"""

    class Meta:
        table_name = 'rbac_menu'

    name = CharField(max_length=32,verbose_name='菜单名称')
    route = CharField(max_length=256, verbose_name='路由')
    icon = CharField(max_length=64, null=True, verbose_name='icon')
    pid = IntegerField(default=0, verbose_name='上级菜单id')


class RbacMenuPermission(MySQLModel):
    """菜单权限表"""

    class Meta:
        table_name = 'rbac_menu_permission'

    menu_id = IntegerField(verbose_name='菜单id')
    permission_id = IntegerField(verbose_name='权限id')

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = Lyon

"""
##############
##   用户   ##
##############

##############
##   角色   ##
##############

##############
##   权限   ##
##############

##############
##   菜单   ##
##############
"""

import hashlib

from lib.token import jwt_decode, jwt_encode
from base.db.mysql import MySQLModel, Func
from peewee import IntegerField, CharField, SmallIntegerField, FloatField


class RbacUser(MySQLModel):
    """用户表"""

    class Meta:
        table_name = 'rbac_user'

    USING_NO = 0
    USING_IS = 1

    nickname = CharField(max_length=32, verbose_name='昵称')
    phone = CharField(max_length=11, verbose_name='手机号')
    password = CharField(max_length=64, verbose_name='密码')
    email = CharField(max_length=64, verbose_name='邮箱')
    is_using = SmallIntegerField(default=USING_IS, verbose_name='是否禁用')

    def encode_token(self):
        from base import app
        data = {
            'id': self.id
        }
        return jwt_encode(data, app.config.SECRET_KEY)

    @classmethod
    def decode_token(cls, token):
        from base import app
        return jwt_decode(token, app.config.SECRET_KEY).get('data')


class RbacRole(MySQLModel):
    """角色表"""

    class Meta:
        table_name = 'rbac_role'

    name = CharField(max_length=32, verbose_name='角色名称')


class RbacUserRole(MySQLModel):
    """用户拥有的角色关系表"""

    class Meta:
        table_name = 'rbac_user_role'

    user_id = IntegerField(verbose_name='用户id')
    role_id = IntegerField(verbose_name='角色id')


class RbacPermission(MySQLModel):
    """权限表"""

    class Meta:
        table_name = 'rbac_permission'

    name = CharField(max_length=32, verbose_name='权限名称')
    alias = CharField(max_length=64, verbose_name='权限别名')


class RbacRolePermission(MySQLModel):
    """角色权限关系表"""

    class Meta:
        table_name = 'rbac_role_permission'

    role_id = IntegerField(verbose_name='角色id')
    permission_id = IntegerField(verbose_name='权限id')


class RbacMenu(MySQLModel):
    """菜单表"""

    class Meta:
        table_name = 'rbac_menu'

    name = CharField(max_length=32,verbose_name='菜单名称')
    route = CharField(max_length=256, verbose_name='路由')
    icon = CharField(max_length=64, null=True, verbose_name='icon')
    pid = IntegerField(default=0, verbose_name='上级菜单id')


class RbacMenuPermission(MySQLModel):
    """菜单权限表"""

    class Meta:
        table_name = 'rbac_menu_permission'

    menu_id = IntegerField(verbose_name='菜单id')
    permission_id = IntegerField(verbose_name='权限id')

#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = Lyon ############## ##   用户   ## ##############  ############## ##   角色   ## ##############  ############## ##   权限   ## ##############  ############## ##   菜单   ## ############## 用户表 角色表 用户拥有的角色关系表 权限表 角色权限关系表 菜单表 菜单权限表