Skip to content

Commit

Permalink
user_role
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantLiu committed Jul 9, 2021
1 parent 9de476d commit d386899
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 50 deletions.
3 changes: 3 additions & 0 deletions apps/dgiot_parse/include/dgiot_parse.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
-define(TYPE, <<"PARSE">>).
-define(CACHE(Channel), binary_to_atom(<<?TYPE/binary, Channel/binary>>, utf8)).
-define(DGIOT_PARSE_ETS, dgiot_parse_ets).
-define(ROLE_USER_ETS, role_user_ets).
-define(USER_ROLE_ETS, user_role_ets).
-define(ROLE_PARENT_ETS, role_parent_ets).
83 changes: 63 additions & 20 deletions apps/dgiot_parse/priv/swagger/swagger_parse.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
{
"name": "_Installation",
"description": "部署"

},
{
"name": "_Session",
Expand Down Expand Up @@ -91,18 +90,18 @@
}
],
"definitions": {
"Token":{
"type":"object",
"properties":{
"Token": {
"type": "object",
"properties": {
"appid": {
"description": "app secret",
"type": "string",
"example":"gEsTiaPAjv"
"example": "gEsTiaPAjv"
},
"secret": {
"description": "app secret",
"type": "string",
"example":"VzY0Mjc5NDQxNTg1ODI4NDk1MzYx"
"example": "VzY0Mjc5NDQxNTg1ODI4NDk1MzYx"
}
}
},
Expand Down Expand Up @@ -297,22 +296,22 @@
"username": {
"description": "用户名",
"type": "string",
"example":"dgiot_user"
"example": "dgiot_user"
},
"password": {
"description": "用户密码",
"type": "string",
"example":"dgiot_pwd"
"example": "dgiot_pwd"
},
"phone": {
"description": "手机号",
"type": "string",
"example":"15268887220"
"example": "15268887220"
},
"department": {
"description": "手机号",
"type": "string",
"example":"IjOCg0BC0I"
"example": "IjOCg0BC0I"
}
}
}
Expand Down Expand Up @@ -369,20 +368,20 @@
"username": {
"description": "用户名",
"type": "string",
"example":"dgiot_user"
"example": "dgiot_user"
},
"department": {
"description": "部门ID",
"type": "string",
"example":"IjOCg0BC0I"
"example": "IjOCg0BC0I"
}
}
}
}
],
"responses": {
"201": {
"description": "Returns success",
"description": "Returns success"
},
"400": {
"description": "Bad Request"
Expand Down Expand Up @@ -413,20 +412,20 @@
"username": {
"description": "用户名",
"type": "string",
"example":"dgiot_user"
"example": "dgiot_user"
},
"department": {
"description": "部门ID",
"type": "string",
"example":"IjOCg0BC0I"
"example": "IjOCg0BC0I"
}
}
}
}
],
"responses": {
"201": {
"description": "Returns success",
"description": "Returns success"
},
"400": {
"description": "Bad Request"
Expand Down Expand Up @@ -605,12 +604,12 @@
"desc": {
"description": "应用描述",
"type": "string",
"example":"pump"
"example": "pump"
},
"access_token": {
"description": "访问token",
"type": "string",
"example":"QjEwMDczNjgxNTg1NzMxMjM0MjQ3"
"example": "QjEwMDczNjgxNTg1NzMxMjM0MjQ3"
},
"expires_in": {
"description": "过期时间",
Expand Down Expand Up @@ -661,12 +660,12 @@
"desc": {
"description": "应用描述",
"type": "string",
"example":"pump"
"example": "pump"
},
"access_token": {
"description": "访问token",
"type": "string",
"example":"QjEwMDczNjgxNTg1NzMxMjM0MjQ3"
"example": "QjEwMDczNjgxNTg1NzMxMjM0MjQ3"
},
"expires_in": {
"description": "过期时间",
Expand Down Expand Up @@ -870,6 +869,50 @@
"_User"
]
}
},
"/disableuser": {
"get": {
"summary": "禁用启用账号",
"description": "禁用启用账号",
"parameters": [
{
"in": "query",
"name": "userid",
"required": true,
"description": "账号id",
"default": "2CCcv7e9ul",
"type": "string"
},
{
"in": "query",
"name": "action",
"required": true,
"description": "状态",
"default": "enable",
"type": "string"
}
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"_User"
]
}
}
}
}
118 changes: 117 additions & 1 deletion apps/dgiot_parse/src/dgiot_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@
get_roleid/1,
get_productid/3,
subscribe/2,
send_msg/3
send_msg/3,
load/0,
save_User_Role/2,
del_User_Role/2,
put_User_Role/3
]).

subscribe(Table, Method) ->
Expand Down Expand Up @@ -1032,3 +1036,115 @@ test_graphql() ->
},
%% {"operationName":"Health","variables":{},"query":"query Health {\n health\n}\n"}
graphql(Data).

load() ->
Success = fun(Page) ->
lists:map(fun(X) ->
#{<<"objectId">> := RoleId, <<"parent">> := #{<<"objectId">> := ParentId}} = X,
dgiot_data:insert(?ROLE_PARENT_ETS, RoleId, ParentId),
role_ets(RoleId)
end, Page)
end,
Query = #{<<"keys">> => <<"parent">>},
dgiot_parse_loader:start(<<"_Role">>, Query, 0, 10, 10000, Success).

role_ets(RoleId) ->
UsersQuery =
#{<<"keys">> => <<"objectId">>,
<<"where">> => #{<<"$relatedTo">> => #{
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => RoleId},
<<"key">> => <<"users">>}
}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Users}} when length(Users) > 0 ->
UserIds =
lists:foldl(fun(#{<<"objectId">> := UserId}, Acc) ->
save_RoleIds(UserId, RoleId),
Acc ++ [UserId]
end, [], Users),
dgiot_data:insert(?ROLE_USER_ETS, RoleId, UserIds);
_ -> pass
end.

save_RoleIds(UserId, RoleId) ->
case dgiot_data:get(?USER_ROLE_ETS, UserId) of
not_find ->
dgiot_data:insert(?USER_ROLE_ETS, UserId, [RoleId]);
RoleIds ->
New_RoleIds = dgiot_utils:unique_2(RoleIds ++ [RoleId]),
dgiot_data:insert(?USER_ROLE_ETS, UserId, New_RoleIds)
end.


save_User_Role(UserId, RoleId) ->
case dgiot_data:get(?USER_ROLE_ETS, UserId) of
not_find ->
dgiot_data:insert(?USER_ROLE_ETS, UserId, [RoleId]);
RoleIds ->
New_RoleIds = dgiot_utils:unique_2(RoleIds ++ [RoleId]),
dgiot_data:insert(?USER_ROLE_ETS, UserId, New_RoleIds)
end,

case dgiot_data:get(?ROLE_USER_ETS, RoleId) of
not_find ->
dgiot_data:insert(?ROLE_USER_ETS, RoleId, [UserId]);
UserIds ->
New_UserIds = dgiot_utils:unique_2(UserIds ++ [UserId]),
dgiot_data:insert(?ROLE_USER_ETS, RoleId, New_UserIds)
end.


del_User_Role(UserId, RoleId) ->
case dgiot_data:get(?USER_ROLE_ETS, UserId) of
not_find ->
pass;
RoleIds when length(RoleIds) > 0 ->
dgiot_data:delete(?USER_ROLE_ETS, UserId);
_ ->
pass
end,
case dgiot_data:get(?ROLE_USER_ETS, RoleId) of
not_find ->
pass;
UserIds when length(UserIds) > 0 ->
New_UserIds = lists:delete(UserId, UserIds),
dgiot_data:insert(?ROLE_USER_ETS, RoleId, New_UserIds);
_ ->
pass
end.

put_User_Role(UserId, OldRoleId, NewRoleId) ->
case dgiot_data:get(?USER_ROLE_ETS, UserId) of
not_find ->
pass;
RoleIds when length(RoleIds) > 0 ->
Old_RoleIds = lists:delete(OldRoleId, RoleIds),
New_RoleIds = dgiot_utils:unique_2(Old_RoleIds ++ [NewRoleId]),
dgiot_data:insert(?USER_ROLE_ETS, UserId, New_RoleIds);
_ ->
pass
end,
case dgiot_data:get(?ROLE_USER_ETS, OldRoleId) of
not_find ->
pass;
OldUserIds when length(OldUserIds) > 0 ->
Old_UserIds = lists:delete(UserId, OldUserIds),
dgiot_data:insert(?ROLE_USER_ETS, OldRoleId, Old_UserIds);
_ ->
pass
end,
case dgiot_data:get(?ROLE_USER_ETS, NewRoleId) of
not_find ->
pass;
NewUserIds when length(NewUserIds) > 0 ->
New_UserIds = dgiot_utils:unique_2(NewUserIds ++ [UserId]),
dgiot_data:insert(?ROLE_USER_ETS, NewRoleId, New_UserIds);
_ ->
pass
end.



Loading

0 comments on commit d386899

Please sign in to comment.