Exemple #1
0
        /// <summary>
        /// 执行授权
        /// </summary>
        /// <returns></returns>
        public bool Grant(bool takeAll, params CodePrivilege[] rights)
        {
            OAuthApp app = OAuthAppCache.Get(this._appid);

            if (app == null)
            {
                Alert("未注册的应用");
                return(false);
            }
            string[] scopeCodes = this._scope.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var      scope      = ScopeCache.Get(scopeCodes);

            if (scope == null || scope.Count <= 0)
            {
                Alert("未定义的授权类型");
                return(false);
            }
            var   fac  = UserModuleFactory.GetUserModuleInstance();
            IUser user = fac?.GetUserByID(this._userId);

            if (user == null)
            {
                Alert("用户信息加载失败");
                return(false);
            }
            if (CheckAlreadyAuth(app.Id, user.UserId))
            {
                return(true);
            }
            if (takeAll && (rights == null || rights.Length <= 0))
            {
                var temp = ScopeRightProvider.GetScopeRights(this._scope);
                rights = new CodePrivilege[temp.Count];
                for (int i = 0; i < rights.Length; i++)
                {
                    rights[i] = new CodePrivilege
                    {
                        Id   = temp[i].Right_Id,
                        Type = temp[i].Right_Type
                    };
                }
            }
            this.Auth_Code = Guid.NewGuid().ToString("N");
            //Tauth_Code daCode = new Tauth_Code();
            var daCode = DaoFactory.Tauth_Code();

            daCode.App_Id      = app.Id;
            daCode.Expire_Time = DateTime.Now.AddMinutes(5);
            daCode.Grant_Code  = this.Auth_Code;
            daCode.Scope_Id    = scope.FirstOrDefault().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);
        }