| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- from rest_framework.permissions import BasePermission
- from django.core.cache import cache as default_cache
- from utils.db_utils import MongoDBTools
- class QxzLoginPermission(BasePermission):
- message = "error no permissions"
- def has_permission(self, request, view):
- try:
- auth_info = request.META.get('HTTP_AUTHORIZATION')
- new_token = auth_info.replace('Bearer ', '')
- print('8888888888', request.session['hnyfqxz'])
- old_token = request.session['hnyfqxz']
- if new_token != old_token:
- raise Exception()
- except Exception as e:
- print('999999999999', e)
- return False
- return True
- class RegisterViewPermission(BasePermission):
- def has_permission(self, request, view):
- try:
- user = request.user
- if user['username'] == "管理员":
- return True
- else:
- return False
- except:
- return False
- class ModulePermission(BasePermission):
- def has_permission(self, request, view):
- try:
- user = request.user
- is_superuser = user.get("is_superuser")
- user_modules = user.get("user_modules")
- path = request.path
- if path.startswith("/pestanalysis"):
- if is_superuser == "True" or user_modules == "1":
- return True
- else:
- return False
- except:
- return False
- class QXZDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- qxz_list_cache = default_cache.get(str(uid)+"_qxz_list")
- if qxz_list_cache:
- if device_id in qxz_list_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':5,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- qxz_list_cache = []
- for item in data:
- qxz_list_cache.append(item["device_id"])
- default_cache.set(str(uid)+"_qxz_list", qxz_list_cache,60*5)
- if device_id in qxz_list_cache:
- return True
- else:
- return False
- class ScdDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
- if scd_dict_cache:
- if device_id in scd_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':2,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- scd_dict_cache = {}
- for item in data:
- scd_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
- if device_id in scd_dict_cache:
- return True
- else:
- return False
- class CbdDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
- if cbd_dict_cache:
- if device_id in cbd_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':3,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id',
- 'disable': '$disable'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- cbd_dict_cache = {}
- for item in data:
- cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
- default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
- if device_id in cbd_dict_cache:
- return True
- else:
- return False
- class BzyDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
- if bzy_dict_cache:
- if device_id in bzy_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':7,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- bzy_dict_cache = {}
- for item in data:
- bzy_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
- if device_id in bzy_dict_cache:
- return True
- else:
- return False
- class XycbDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
- if xycb_dict_cache:
- if device_id in xycb_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':4,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- xycb_dict_cache = {}
- for item in data:
- xycb_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_xycb_list", xycb_dict_cache,60*5)
- if device_id in xycb_dict_cache:
- return True
- else:
- return False
- class XctDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
- if xct_dict_cache:
- if device_id in xct_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':12,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- xct_dict_cache = {}
- for item in data:
- xct_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
- if device_id in xct_dict_cache:
- return True
- else:
- return False
- class GssqDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- gssq_dict_cache = default_cache.get(str(uid)+"_gssq_list")
- if gssq_dict_cache:
- if device_id in gssq_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':15,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- gssq_dict_cache = {}
- for item in data:
- gssq_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
- if device_id in gssq_dict_cache:
- return True
- else:
- return False
- class JkDeviceDetailPermission(BasePermission):
- message = "非此账户下设备"
-
- def has_permission(self, request, view):
- uid = request.user
- device_id = request.query_params.get("device_id")
- if device_id is None:
- return True
- jk_dict_cache = default_cache.get(str(uid)+"_jk_list")
- if jk_dict_cache:
- if device_id in jk_dict_cache:
- return True
- else:
- return False
- else:
- wheres = {
- 'device_type_id':6,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- 'id': '$id',
- 'device_id': '$device_id'
- }
- m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
- data = m.find_many(wheres=wheres, options=project)
- xct_dict_cache = {}
- for item in data:
- xct_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_jk_list", xct_dict_cache,60*5)
- if device_id in xct_dict_cache:
- return True
- else:
- return False
|