/// <summary> /// 执行授权 /// </summary> /// <returns></returns> public bool Grant(bool takeAll, params GrantCodeRight[] rights) { OAuthApp app = OAuthAppCache.Instance.Find(it => it.APP_CODE.Equals(this._appCode)); if (app == null) { Alert("未注册的应用"); return(false); } GrantScope[] scope = ScopeCache.Instance.FindAll(this._scope); if (scope == null || scope.Length <= 0) { Alert("未定义的授权类型"); return(false); } var fac = UserModuleFactory.GetUserModuleInstance(); IUser user = fac?.GetUserByCode(this._userCode); if (user == null) { Alert("用户信息加载失败"); return(false); } if (CheckAlreadyAuth(app.APP_ID, user.UserId)) { return(true); } if (takeAll && (rights == null || rights.Length <= 0)) { var temp = ScopeRightProvider.GetScopeRights(this._scope); rights = new GrantCodeRight[temp.Count]; for (int i = 0; i < rights.Length; i++) { rights[i] = new GrantCodeRight { RightId = temp[i].Right_Id, RightType = temp[i].Right_Type }; } } this.Auth_Code = Guid.NewGuid().ToString("N"); Tauth_Code daCode = new Tauth_Code(); daCode.App_Id = app.APP_ID; daCode.Expire_Time = DateTime.Now.AddMinutes(5); daCode.Grant_Code = this.Auth_Code; daCode.Scope_Id = scope.FirstOrDefault().SCOPE_ID; daCode.User_Id = user.UserId; daCode.Device_Id = this._device_id; if (rights != null && rights.Length > 0) { daCode.Right_Json = Javirs.Common.Json.JsonSerializer.JsonSerialize(rights); } if (!daCode.Insert()) { Alert("授权失败,请重试!"); return(false); } return(true); }
public static int ForceLoadUserGrantRight(int appId, int userId, string scope, out IEnumerable <int>[] value) { value = new List <int> [2]; try { Tauth_Token daToken = new Tauth_Token(); if (!daToken.SelectByAppId_UserId(appId, userId)) { Log.Info("未找到授权记录"); return(-1); } string[] scopeArray = null; if (scope.Contains(",")) { scopeArray = scope.Split(','); } else { scopeArray = new string[] { scope }; } var scopeRights = ScopeRightProvider.GetScopeApis(scopeArray); //如果作用域不包含任何权限(仅OpenID),返回已经授权过 if (scopeRights == null || scopeRights.Count <= 0) { Log.Info("授权作用域不包含任何权限"); return(1); } Log.Info("授权作用域包含权限数量{0}", scopeRights.Count); var tmp = new List <int>(); foreach (var sr in scopeRights) { tmp.Add(sr.Api_Id); } value[0] = tmp; //value[0] = scopeRights.Select(it => it.Api_Id); Tauth_Token_RightCollection daRightCollection = new Tauth_Token_RightCollection(); daRightCollection.ListEffectiveByTokenId(daToken.Token_Id); List <TokenRightApi> apis = MapProvider.Map <TokenRightApi>(daRightCollection.DataTable); Log.Info("已经获得的权限有{0}个", apis?.Count); var tmp2 = new List <int>(); foreach (var a in apis) { tmp2.Add(a.Api_Id); } value[1] = tmp2; return(0); } catch (Exception ex) { Log.Error("检查是否已授权出现错误", ex); return(-1); } }