Beispiel #1
0
    def signup_3rd(self, appid, openid, userid=None):
        try:
            where = {
                'appid':appid,
                'openid':openid,
            }
            user_data = {
                'password': '',
                'ctime': int(time.time()),
                'status': STATUS_OK,  # 默认状态为2
            }

            lastid = -1
            with get_connection(self.dbname) as conn:
                ret = conn.select_one('openuser', where=where, fields='userid')
                if ret:
                    return ERR_USER, 'user exist'

                if userid:
                    ret = conn.select_one(self.table, where={'id':userid}, fields='id')
                    if not ret:
                        return ERR_USER, 'user error'

                
                if not userid:
                    user_data['id'] = createid.new_id64(conn=conn)
                    user_data['username'] = '******' % (user_data['id'])
                    conn.insert(self.table, user_data)

                openuser_data = {
                    'id': createid.new_id64(conn=conn),
                    'userid':user_data['id'],
                    'appid':appid,
                    'openid':openid,
                    'plat':config.OPENUSER_ACCOUNT[appid]['plat'],
                    'ctime':DBFunc('UNIX_TIMESTAMP(now())'),
                }
                conn.insert('openuser', openuser_data)
          
            # 没有session才能创建新session
            if not self.ses:
                self.create_session()
                log.debug('create sesssion:%s', self.ses.sid)
                sesdata = {
                    'userid':int(user_data['id']), 
                    'username':'', 
                    'isadmin':0, 
                    'status':user_data['status'],
                    'appid':appid,
                    'openid':openid,
                    'plat':config.OPENUSER_ACCOUNT[appid]['plat'],
                }
                self.ses.update(sesdata)

            retcode, userinfo = self.get_user(user_data['id'])
            return OK, userinfo
        except Exception as e:
            log.error(traceback.format_exc())
            return self.fail(ERR, 'error:' + str(e))
Beispiel #2
0
    def perm_give(self):
        permid = self.data.get('permid')

        values = []
        if isinstance(permid, (list, tuple)):
            for p in permid:
                values.append({
                    'permid': p,
                    'roleid': self.data.get('id'),
                    'ctime': DBFunc('UNIX_TIMESTAMP(now())'),
                    'utime': DBFunc('UNIX_TIMESTAMP(now())')
                })
        else:
            values.append({
                'permid': permid,
                'roleid': self.data.get('id'),
                'ctime': DBFunc('UNIX_TIMESTAMP(now())'),
                'utime': DBFunc('UNIX_TIMESTAMP(now())')
            })

        with get_connection(self.dbname) as conn:
            for value in values:
                value['id'] = createid.new_id64(conn=conn)
                ret = conn.insert('role_perm', value)

        return OK, {'rows': len(values)}
Beispiel #3
0
    def group_join(self):
        isadmin = self.ses['isadmin']
        userid = self.ses['userid']
        in_userid = self.data.get('userid')
        
        if in_userid:
            if not isadmin:
                return ERR_PERM, 'permission deny'
            else:
                userid = in_userid
        
        groupid = self.data.get('groupid')
       
        t = int(time.time())
        with get_connection(self.dbname) as conn:
            data = {
                'id': createid.new_id64(conn=conn),
                'userid':userid,
                'groupid':groupid,
                'ctime':t,
                'utime':t,
            }
            ret = conn.insert('user_group', data)
            if ret != 1:
                return ERR_DB, 'error'

            ret = conn.select_one('user_group', where={'id':data['id']})
            for k in ['id','userid','groupid']:
                ret[k] = str(ret[k])
            return OK, ret
Beispiel #4
0
    def perm_give(self):
        isadmin = self.ses.get('isadmin', 0)
        userid = self.ses['userid']
        in_userid = self.data.get('userid')
        
        if in_userid:
            if not isadmin:
                return ERR_PERM, 'permission deny'
            else:
                userid = in_userid
 
        roleid = self.data.get('roleid')
        permid = self.data.get('permid')
            
        items = []
        for k in ['roleid', 'permid']:
            v = self.data.get(k)
            if not v:
                continue
            log.debug('%s: %s', k, v)
            if isinstance(v, (list,tuple)):
                for one in v:
                    data = {'roleid':0, 'permid':0}
                    data[k] = one
                    items.append(data)
            else:
                data = {'roleid':0, 'permid':0}
                data[k] = v
                items.append(data)

        t = int(time.time())
        with get_connection(self.dbname) as conn:
            ids = []
            for item in items:
                data = {
                    'id': createid.new_id64(conn=conn),
                    'userid':userid,
                    'roleid':item['roleid'],
                    'permid':item['permid'],
                    'ctime':t,
                    'utime':t,
                }
                ids.append(data['id'])

                ret = conn.insert('user_perm', data)
                if ret != 1:
                    return ERR_DB, 'error'

            ret = conn.select('user_perm', where={'id':('in', ids)})
            for row in ret:
                for k in ['id','userid','roleid','permid']:
                    row[k] = str(row[k])

            return OK, ret
Beispiel #5
0
    def signup(self):
        log.info('data:%s', self.data)
        
        email    = self.data.get('email','')
        mobile   = self.data.get('mobile','')
        username = self.data.get('username','')
        password = self.data.get('password','')
        pass_enc = create_password(password)

        try:
            where = {}
            insertdata = {
                'password': pass_enc,
                'ctime': int(time.time()),
                'status': STATUS_OK,  # 默认状态为2
            }
            if email:
                where['email'] = email
                insertdata['email'] = email
            if mobile:
                where['mobile'] = mobile
                insertdata['mobile'] = mobile
            if username:
                where['username'] = username
                insertdata['username'] = username

            if not email and not mobile:
                return ERR_PARAM, 'email/mobile must not null'

            lastid = -1
            with get_connection(self.dbname) as conn:
                ret = conn.select(self.table, where, 'id')
                if len(ret) >= 1:
                    return ERR_USER, 'username or email or mobile exist'
                insertdata['id'] = createid.new_id64(conn=conn)
                conn.insert(self.table, insertdata)
          
            # 没有session才能创建新session
            if not self.ses:
                self.create_session()
                log.debug('create sesssion:%s', self.ses.sid)
                sesdata = {
                    'userid':int(insertdata['id']), 
                    'username':username, 
                    'isadmin':0, 
                    'status':insertdata['status'],
                }
                self.ses.update(sesdata)

            retcode, userinfo = self.get_user(insertdata['id'])
            return OK, userinfo
        except Exception as e:
            log.error(traceback.format_exc())
            return self.fail(ERR, 'error:' + str(e))