internal void SetRole(int AniDBUserId, WebCache_RoleType rt)
        {
            lock (_lock)
            {
                WebCache_Role b = _db.Roles.FirstOrDefault(a => a.AniDBUserId == AniDBUserId);
                if (rt == WebCache_RoleType.None)
                {
                    //Kill Role
                    if (b != null)
                    {
                        _db.Remove(b);
                    }
                }
                else if (b == null)
                {
                    //Create rtole
                    b             = new WebCache_Role();
                    b.AniDBUserId = AniDBUserId;
                    _db.Add(b);
                }
                else
                {
                    //Update role
                    b.Type = rt;
                }

                _db.SaveChanges();
                Dictionary <int, WebCache_Role> roles = _db.Roles.ToDictionary(a => a.AniDBUserId, a => a);
                _mc.Set("roles", roles, TimeSpan.FromSeconds(60));
            }
        }
示例#2
0
 public bool SetRole(int anidbuserid, WebCache_RoleType rt)
 {
     return(WrapAuthentication((token) =>
     {
         cclient.SetRole(token, anidbuserid, (int)rt);
     }));
 }
        public async Task <IActionResult> SetRole(string token, int anidbuserid, int role)
        {
            try
            {
                SessionInfoWithError s = await VerifyTokenAsync(token);

                if (s.Error != null)
                {
                    return(s.Error);
                }
                if ((s.Role & WebCache_RoleType.Admin) == 0)
                {
                    return(StatusCode(403, "Admin Only"));
                }
                WebCache_User us = await _db.Users.FirstOrDefaultAsync(a => a.AniDBUserId == anidbuserid);

                if (us == null)
                {
                    return(StatusCode(404, "User not found"));
                }
                WebCache_RoleType rt = (WebCache_RoleType)role;
                SetRole(anidbuserid, rt);
                return(Ok());
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"SETROLE with Token={token} Userid={anidbuserid} Role={(WebCache_RoleType)role}");
                return(StatusCode(500));
            }
        }
示例#4
0
        private async Task AddProviderInternal(SessionInfoWithError s, WebCache_CrossRef_AniDB_Provider cross, bool?approve)
        {
            Models.Database.WebCache_CrossRef_AniDB_Provider r = await _db.CrossRef_AniDB_Providers.FirstOrDefaultAsync(a => a.AniDBUserId == s.AniDBUserId && a.AnimeID == cross.AnimeID && a.CrossRefType == cross.CrossRefType);

            if (r == null)
            {
                r = new Models.Database.WebCache_CrossRef_AniDB_Provider();
                _db.Add(r);
            }

            WebCache_RoleType rt = GetRole(s.AniDBUserId);

            r.FillWith(cross);
            //If user is Admin, and this come with approve flag, let approve it, and clean any other approval from the db
            if ((rt & WebCache_RoleType.Admin) > 0 && approve.HasValue && approve.Value)
            {
                r.Approved = WebCache_RoleType.Admin;
                List <Models.Database.WebCache_CrossRef_AniDB_Provider> reset_admins = await _db.CrossRef_AniDB_Providers.Where(a => a.AnimeID == cross.AnimeID && a.CrossRefType == cross.CrossRefType && a.AniDBUserId != s.AniDBUserId && a.Approved == WebCache_RoleType.Admin).ToListAsync();

                foreach (Models.Database.WebCache_CrossRef_AniDB_Provider w in reset_admins)
                {
                    w.Approved = WebCache_RoleType.None;
                }
            }
            //If moderator, simple tag it.
            else if ((rt & WebCache_RoleType.Moderator) > 0)
            {
                r.Approved = WebCache_RoleType.Moderator;
            }
            else
            {
                r.Approved = WebCache_RoleType.None;
            }

            r.AniDBUserId = s.AniDBUserId;
        }