예제 #1
0
        public async Task <ActionResult <BaseResponse> > AddPlcSecurityAsync([FromBody] PlcSecurityAddDto req)
        {
            //验证是否是合法用户,并且用户是否有plc权限
            //超级管理员有权限
            var    GroupId = User.Claims.FirstOrDefault(a => a.Type == "GroupId").Value;
            var    isAdmin = User.Claims.FirstOrDefault(a => a.Type == "IsAdmin").Value.ToLower() == "true" ? true : false;
            string Code    = User.Claims.FirstOrDefault(a => a.Type == "Code").Value;
            string Account = User.Claims.FirstOrDefault(a => a.Type == "Account").Value;
            var    Roles   = User.Claims.FirstOrDefault(a => a.Type == "Role").Value.ToString();

            if (string.IsNullOrWhiteSpace(Roles))
            {
                return(Unauthorized("用户没有权限"));
            }
            var list = Array.ConvertAll <string, int>(Roles.Split(','), s => int.Parse(s));

            if (!isAdmin)//非管理员验证用户是否有plc权限
            {
                var ret = await _rs.IsExist(a => a.RoleName == "PLC秘钥生成" && list.Contains(a.Id));

                if (!ret)
                {
                    return(Forbid("用户没有权限操作"));
                }
            }
            var data = await _plc.AddPlcSecurityAsync(Account, req);

            return(data);
        }
예제 #2
0
        /// <summary>
        /// 添加plc鉴权码,返回鉴权码
        /// </summary>
        /// <param name="Account">操作人</param>
        /// <param name="req">鉴权码数据</param>
        /// <returns>返回鉴权码</returns>
        public async Task <BaseResponse> AddPlcSecurityAsync(string Account, PlcSecurityAddDto req)
        {
            try
            {
                req.SecurityKey = CreateKey(req.SecurityKey);
                var entity = _mapper.Map <PlcSecurityModel>(req);
                entity.Create = Account;
                //entity.SecurityKey = CreateKey(req.SecurityKey);
                await _psr.AddAsync(entity);

                _log.LogInformation($"{Account}生成{entity.SecurityKey}PLC鉴权码成功");
                return(new HandleResponse <string> {
                    Success = true, Message = "生成PLC鉴权码成功", Key = entity.SecurityKey
                });
            }
            catch (Exception ex)
            {
                _log.LogError($"{Account}添加PLC鉴权码失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "生成PLC鉴权码失败"
                });
            }
        }