def group_add_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_username = request.POST.get('member_username', '') member_user = GsuserManager.get_user_by_name(member_username) if not member_user: return json_failed(500, u'没有该用户名') teamMember = TeamManager.get_teamMember_by_teamUserId_userId( teamUser.id, member_user.id) if not teamMember: return json_failed( 500, u'用户 %s 还没有加入团队帐号 %s' % (member_user.username, teamUser.username)) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId( teamGroup.id, member_user.id) if groupMember: return json_success(u'用户 %s 已经在该组' % member_user.username) groupMember = GroupMember(team_user_id=teamUser.id, group_id=teamGroup.id, member_user_id=member_user.id) groupMember.save() return json_success(u'成功添加用户 %s 到组 %s' % (member_user.username, teamGroup.name))
def add_member(request, username): (teamUser, teamUserprofile) = _get_team_user_userprofile(request, username) teamMember = None username_or_email = request.POST.get('username_or_email', '') if '@' in username_or_email: user = GsuserManager.get_user_by_email(username_or_email) if not user: ref_hash = '%032x' % random.getrandbits(128) ref_message = u'用户 %s 邀请您注册Gitshell,成为团队 %s 的成员' % ( request.user.username, username) userViaRef = UserViaRef(email=username_or_email, ref_type=REF_TYPE.VIA_TEAM_MEMBER, ref_hash=ref_hash, ref_message=ref_message, first_refid=teamUser.id, first_refname=teamUser.username) userViaRef.save() join_url = 'https://gitshell.com/join/ref/%s/' % ref_hash Mailer().send_join_via_team_addmember(request.user, teamUser, username_or_email, join_url) return json_failed(301, u'邮箱 %s 未注册,已经发送邮件邀请对方注册' % username_or_email) teamMember = TeamManager.add_teamMember_by_email( teamUser, username_or_email) else: teamMember = TeamManager.add_teamMember_by_username( teamUser, username_or_email) if not teamMember: return json_failed(404, u'没有相关用户,不能是团队帐号') return json_success(u'成功添加用户')
def unwatch(request, user_name): gsuserprofile = GsuserManager.get_userprofile_by_name(user_name) if gsuserprofile is None: message = u'用户不存在' return json_failed(404, message) if not RepoManager.unwatch_user(request.userprofile, gsuserprofile): message = u'取消关注失败,可能用户未被关注' return json_failed(500, message) return json_success(u'成功取消关注用户 %s' % user_name)
def watch(request, user_name): gsuserprofile = GsuserManager.get_userprofile_by_name(user_name) if gsuserprofile is None: message = u'用户不存在' return json_failed(404, message) if not RepoManager.watch_user(request.userprofile, gsuserprofile): message = u'关注失败,关注数量超过限制或者用户不允许关注' return json_failed(500, message) return json_success(u'成功关注用户 %s' % user_name)
def group_add(request, username): teamUser = GsuserManager.get_user_by_name(username) group_name = request.POST.get('group_name', '') if not re.match('^[ 0-9a-zA-Z_-]+$', group_name): return json_failed(403, u'组名不符合规范,只允许[ 0-9a-zA-Z_-]之内的字符') teamGroup = TeamManager.get_teamGroup_by_teamUserId_name(teamUser.id, group_name) if teamGroup: return json_failed(500, u'组名已经存在') teamGroup = TeamGroup(team_user_id = teamUser.id, name = group_name, desc = '') teamGroup.save() return json_success(u'成功创建组 %s' % group_name)
def group_add(request, username): teamUser = GsuserManager.get_user_by_name(username) group_name = request.POST.get('group_name', '') if not re.match('^[ 0-9a-zA-Z_-]+$', group_name): return json_failed(403, u'组名不符合规范,只允许[ 0-9a-zA-Z_-]之内的字符') teamGroup = TeamManager.get_teamGroup_by_teamUserId_name( teamUser.id, group_name) if teamGroup: return json_failed(500, u'组名已经存在') teamGroup = TeamGroup(team_user_id=teamUser.id, name=group_name, desc='') teamGroup.save() return json_success(u'成功创建组 %s' % group_name)
def email_add(request): is_verify = 0 email = request.POST.get('email') userEmail = GsuserManager.add_useremail(request.user, email, is_verify) if not userEmail: return json_failed(500, u'绑定邮箱个数最多50个,确定邮箱格式正确和未被绑定') return json_success(u'成功添加邮箱 %s' % email)
def cancal_admin(request, username): (manage_teamMember, teamMember) = _get_teamMember_by_manageTeamMemberId(request) teamMembers = TeamManager.list_teamMember_by_teamUserId(teamMember.team_user_id) if _has_other_admin_teamMember(request, manage_teamMember, teamMembers): manage_teamMember.is_admin = 0 manage_teamMember.save() return json_success(u'解除管理员权限') return json_failed(500, u'解除管理员失败,一个团队帐号至少需要保留一个管理员')
def remove_member(request, username): (manage_teamMember, teamMember) = _get_teamMember_by_manageTeamMemberId(request) teamMembers = TeamManager.list_teamMember_by_teamUserId(teamMember.team_user_id) if _has_other_admin_teamMember(request, manage_teamMember, teamMembers): manage_teamMember.visibly = 1 manage_teamMember.save() return json_success(u'删除用户成功') return json_failed(500, u'删除用户失败,一个团队帐号至少需要保留一个管理员')
def member_leave(request, username): (teamUser, teamUserprofile) = _get_team_user_userprofile(request, username) teamMember = TeamManager.get_teamMember_by_teamUserId_userId(teamUser.id, request.user.id) teamMembers = TeamManager.list_teamMember_by_teamUserId(teamMember.team_user_id) if _has_other_admin_teamMember(request, teamMember, teamMembers): teamMember.visibly = 1 teamMember.save() return json_success(u'用户退出成功') return json_failed(500, u'用户退出失败,一个团队帐号至少需要保留一个管理员')
def cancal_admin(request, username): (manage_teamMember, teamMember) = _get_teamMember_by_manageTeamMemberId(request) teamMembers = TeamManager.list_teamMember_by_teamUserId( teamMember.team_user_id) if _has_other_admin_teamMember(request, manage_teamMember, teamMembers): manage_teamMember.is_admin = 0 manage_teamMember.save() return json_success(u'解除管理员权限') return json_failed(500, u'解除管理员失败,一个团队帐号至少需要保留一个管理员')
def group_add_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_username = request.POST.get('member_username', '') member_user = GsuserManager.get_user_by_name(member_username) if not member_user: return json_failed(500, u'没有该用户名') teamMember = TeamManager.get_teamMember_by_teamUserId_userId(teamUser.id, member_user.id) if not teamMember: return json_failed(500, u'用户 %s 还没有加入团队帐号 %s' % (member_user.username, teamUser.username)) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId(teamGroup.id, member_user.id) if groupMember: return json_success(u'用户 %s 已经在该组' % member_user.username) groupMember = GroupMember(team_user_id=teamUser.id, group_id=teamGroup.id, member_user_id=member_user.id) groupMember.save() return json_success(u'成功添加用户 %s 到组 %s' % (member_user.username, teamGroup.name))
def remove_member(request, username): (manage_teamMember, teamMember) = _get_teamMember_by_manageTeamMemberId(request) teamMembers = TeamManager.list_teamMember_by_teamUserId( teamMember.team_user_id) if _has_other_admin_teamMember(request, manage_teamMember, teamMembers): manage_teamMember.visibly = 1 manage_teamMember.save() return json_success(u'删除用户成功') return json_failed(500, u'删除用户失败,一个团队帐号至少需要保留一个管理员')
def add_member(request, username): (teamUser, teamUserprofile) = _get_team_user_userprofile(request, username) teamMember = None username_or_email = request.POST.get('username_or_email', '') if '@' in username_or_email: user = GsuserManager.get_user_by_email(username_or_email) if not user: ref_hash = '%032x' % random.getrandbits(128) ref_message = u'用户 %s 邀请您注册Gitshell,成为团队 %s 的成员' % (request.user.username, username) userViaRef = UserViaRef(email=username_or_email, ref_type=REF_TYPE.VIA_TEAM_MEMBER, ref_hash=ref_hash, ref_message=ref_message, first_refid = teamUser.id, first_refname = teamUser.username) userViaRef.save() join_url = 'https://gitshell.com/join/ref/%s/' % ref_hash Mailer().send_join_via_team_addmember(request.user, teamUser, username_or_email, join_url) return json_failed(301, u'邮箱 %s 未注册,已经发送邮件邀请对方注册' % username_or_email) teamMember = TeamManager.add_teamMember_by_email(teamUser, username_or_email) else: teamMember = TeamManager.add_teamMember_by_username(teamUser, username_or_email) if not teamMember: return json_failed(404, u'没有相关用户,不能是团队帐号') return json_success(u'成功添加用户')
def member_leave(request, username): (teamUser, teamUserprofile) = _get_team_user_userprofile(request, username) teamMember = TeamManager.get_teamMember_by_teamUserId_userId( teamUser.id, request.user.id) teamMembers = TeamManager.list_teamMember_by_teamUserId( teamMember.team_user_id) if _has_other_admin_teamMember(request, teamMember, teamMembers): teamMember.visibly = 1 teamMember.save() return json_success(u'用户退出成功') return json_failed(500, u'用户退出失败,一个团队帐号至少需要保留一个管理员')
def email_primary(request, eid): usermail = GsuserManager.get_useremail_by_id(eid) if not usermail or usermail.user_id != request.user.id: return json_failed(500, u'设置失败,没有权限') useremails = GsuserManager.list_useremail_by_userId(request.user.id) for x in useremails: if usermail.id != x.id and x.is_primary == 1: x.is_primary = 0 x.save() usermail.is_primary = 1 usermail.save() return json_success(u'成功设置默认邮箱 %s' % usermail.email)
def wrap(request, *args, **kwargs): if len(args) >= 2: user_name = args[0]; repo_name = args[1] repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: return error_with_reason(request, 'repo_not_found') if repo.auth_type != 0 and not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) is_allowed_access_repo = RepoManager.is_allowed_access_repo(repo, request.user, REPO_PERMISSION.READ_ONLY) if not is_allowed_access_repo: if request.method == 'POST': return json_failed(403, u'没有管理权限') return error_with_reason(request, 'repo_permission_denied') if request.user.is_authenticated(): feedAction = FeedAction() feedAction.add_recently_view_repo_now(request.user.id, repo.id) return function(request, *args, **kwargs)
def wrap(request, *args, **kwargs): if len(args) >= 2: user_name = args[0]; repo_name = args[1] repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: return error_with_reason(request, 'repo_not_found') if not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) from gitshell.team.models import TeamManager user_permission = TeamManager.get_repo_user_permission(repo, request.user) if user_permission < REPO_PERMISSION.ADMIN: if user_permission >= REPO_PERMISSION.WEB_VIEW: return HttpResponseRedirect('/%s/%s/' % (user_name, repo_name)) if request.method == 'POST': return json_failed(403, u'没有管理权限') return error_with_reason(request, 'repo_permission_denied') if request.user.is_authenticated(): feedAction = FeedAction() feedAction.add_recently_view_repo_now(request.user.id, repo.id) return function(request, *args, **kwargs)
def wrap(request, *args, **kwargs): if len(args) >= 2: user_name = args[0] repo_name = args[1] repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: return error_with_reason(request, 'repo_not_found') if repo.auth_type != 0 and not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) is_allowed_access_repo = RepoManager.is_allowed_access_repo( repo, request.user, REPO_PERMISSION.READ_ONLY) if not is_allowed_access_repo: if request.method == 'POST': return json_failed(403, u'没有管理权限') return error_with_reason(request, 'repo_permission_denied') if request.user.is_authenticated(): feedAction = FeedAction() feedAction.add_recently_view_repo_now(request.user.id, repo.id) return function(request, *args, **kwargs)
def wrap(request, *args, **kwargs): if len(args) >= 2: user_name = args[0] repo_name = args[1] repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: return error_with_reason(request, 'repo_not_found') if not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) from gitshell.team.models import TeamManager user_permission = TeamManager.get_repo_user_permission( repo, request.user) if user_permission < REPO_PERMISSION.ADMIN: if user_permission >= REPO_PERMISSION.WEB_VIEW: return HttpResponseRedirect('/%s/%s/' % (user_name, repo_name)) if request.method == 'POST': return json_failed(403, u'没有管理权限') return error_with_reason(request, 'repo_permission_denied') if request.user.is_authenticated(): feedAction = FeedAction() feedAction.add_recently_view_repo_now(request.user.id, repo.id) return function(request, *args, **kwargs)
def _response_not_admin_rights(request): if request.method == 'POST': return json_failed(403, u'没有管理员权限') return error_with_reason(request, 'repo_permission_denied')
def _response_not_manage_rights(request): return json_failed(403, u'没有管理员权限')