Consolidate mod permissions checks in is_moderator.

Also moves the modbox down one chunk.
This commit is contained in:
Neil Williams
2012-04-27 12:03:33 -07:00
parent c15ff9fa86
commit 7689d9c01d
4 changed files with 31 additions and 32 deletions

View File

@@ -396,16 +396,13 @@ class FrontController(RedditController):
action=VOneOf('type', ModAction.actions))
@api_doc(api_section.moderation)
def GET_moderationlog(self, num, after, reverse, count, mod, action):
if not c.user_is_loggedin:
if not c.user_is_loggedin or not (c.user_is_admin or
c.site.is_moderator(c.user)):
return self.abort404()
if isinstance(c.site, (MultiReddit, ModSR)):
srs = Subreddit._byID(c.site.sr_ids, return_dict=False)
# check that user is mod on all requested srs
if not Subreddit.user_mods_all(c.user, srs) and not c.user_is_admin:
return self.abort404()
# grab all moderators
mod_ids = set(Subreddit.get_all_mod_ids(srs))
mods = Account._byID(mod_ids, data=True)
@@ -415,8 +412,6 @@ class FrontController(RedditController):
elif isinstance(c.site, FakeSubreddit):
return self.abort404()
else:
if not c.site.is_moderator(c.user) and not c.user_is_admin:
return self.abort404()
mod_ids = c.site.moderators
mods = Account._byID(mod_ids, data=True)
@@ -606,8 +601,7 @@ class FrontController(RedditController):
return self._edit_modcontrib_reddit(location, num, after, reverse,
count, created)
elif isinstance(c.site, MultiReddit):
srs = Subreddit._byID(c.site.sr_ids, return_dict=False)
if not Subreddit.user_mods_all(c.user, srs) and not c.user_is_admin:
if not (c.user_is_admin or c.site.is_moderator(c.user)):
self.abort403()
return self._edit_modcontrib_reddit(location, num, after, reverse,
count, created)

View File

@@ -769,7 +769,7 @@ class MessageController(ListingController):
elif self.where == 'sent':
q = queries.get_sent(c.user)
elif self.where == 'multi' and self.subwhere == 'unread':
q = queries.merge_results(*[queries.get_unread_subreddit_messages(s) for s in self.srs])
q = queries.merge_results(*[queries.get_unread_subreddit_messages(s) for s in c.site.kept_srs])
elif self.where == 'moderator' and self.subwhere == 'unread':
if c.default_sr:
srids = Subreddit.reverse_moderator_ids(c.user)
@@ -804,11 +804,9 @@ class MessageController(ListingController):
if not (c.default_sr or c.site.is_moderator(c.user) or c.user_is_admin):
abort(403, "forbidden")
if isinstance(c.site, MultiReddit):
srs = Subreddit._byID(c.site.sr_ids, data=False, return_dict=False)
if not (c.user_is_admin or Subreddit.user_mods_all(c.user, srs)):
if not (c.user_is_admin or c.site.is_moderator(c.user)):
self.abort403()
self.where = "multi"
self.srs = srs
elif isinstance(c.site, ModSR) or not c.default_sr:
self.where = "moderator"
else:

View File

@@ -253,17 +253,12 @@ class Reddit(Templated):
if c.user.pref_show_sponsorships or not c.user.gold:
ps.append(SponsorshipBox())
if (c.user_is_loggedin and (isinstance(c.site, ModSR) or
c.site.is_moderator(c.user))):
ps.append(self.sr_admin_menu())
no_ads_yet = True
if isinstance(c.site, (MultiReddit, ModSR)) and c.user_is_loggedin:
srs = Subreddit._byID(c.site.sr_ids,data=True,
return_dict=False)
if (isinstance(c.site, MultiReddit) and
Subreddit.user_mods_all(c.user, srs)):
if c.user_is_admin or c.site.is_moderator(c.user):
ps.append(self.sr_admin_menu())
if srs:
@@ -272,6 +267,9 @@ class Reddit(Templated):
# don't show the subreddit info bar on cnames unless the option is set
if not isinstance(c.site, FakeSubreddit) and (not c.cname or c.site.show_cname_sidebar):
ps.append(SubredditInfoBar())
if c.user_is_loggedin and (c.user_is_admin or
c.site.is_moderator(c.user)):
ps.append(self.sr_admin_menu())
if (c.user.pref_show_adbox or not c.user.gold) and not g.disable_ads:
ps.append(Ads())
no_ads_yet = False

View File

@@ -640,17 +640,6 @@ class Subreddit(Thing, Printable):
def __ne__(self, other):
return not self.__eq__(other)
@staticmethod
def user_mods_all(user, srs):
# Get moderator SRMember relations for all in srs
# if a relation doesn't exist there will be a None entry in the
# returned dict
mod_rels = SRMember._fast_query(srs, user, 'moderator', data=False)
if None in mod_rels.values():
return False
else:
return True
@staticmethod
def get_all_mod_ids(srs):
from r2.lib.db.thing import Merge
@@ -821,6 +810,9 @@ class _DefaultSR(FakeSubreddit):
path = '/'
header = g.default_header_url
def is_moderator(self, user):
return False
def get_links_sr_ids(self, sr_ids, sort, time):
from r2.lib.db import queries
from r2.models import Link
@@ -906,15 +898,29 @@ class MultiReddit(_DefaultSR):
self.real_path = path
self.sr_ids = sr_ids
srs = Subreddit._byID(self.sr_ids, return_dict=False)
self.srs = Subreddit._byID(self.sr_ids, return_dict=False)
self.banned_sr_ids = []
self.kept_sr_ids = []
for sr in srs:
for sr in self.srs:
if sr._spam:
self.banned_sr_ids.append(sr._id)
else:
self.kept_sr_ids.append(sr._id)
def is_moderator(self, user):
if not user:
return False
# Get moderator SRMember relations for all in srs
# if a relation doesn't exist there will be a None entry in the
# returned dict
mod_rels = SRMember._fast_query(self.srs, user,
'moderator', data=False)
if None in mod_rels.values():
return False
else:
return True
@property
def path(self):
return '/r/' + self.real_path
@@ -968,6 +974,9 @@ class ModSR(ModContribSR):
query_param = "moderator"
real_path = "mod"
def is_moderator(self, user):
return True
class ContribSR(ModContribSR):
name = "contrib"
title = "communities you're approved on"