Ejemplo n.º 1
0
        public void JoinPublicCommunity()
        {
            context.AccessLevel = EntityAccessLevel.Privilege;
            var usr1 = User.FromUsername(context, "testusr1");
            var usr2 = User.FromUsername(context, "testusr2");

            context.StartImpersonation(usr1.Id);

            try {
                ThematicCommunity community = ThematicCommunity.FromIdentifier(context, "community-public-1");
                var roles = Role.GetUserRolesForDomain(context, usr1.Id, community.Id);

                //user not part of community
                Assert.AreEqual(0, roles.Length);

                community.TryJoinCurrentUser();
                roles = Role.GetUserRolesForDomain(context, usr1.Id, community.Id);

                //user part of community
                Assert.AreEqual(1, roles.Length);
                Assert.AreEqual(RoleTep.MEMBER, roles [0].Name);

                //check user cannot joins twice
                community.TryJoinCurrentUser();
                roles = Role.GetUserRolesForDomain(context, usr1.Id, community.Id);
                Assert.AreEqual(1, roles.Length);

                context.EndImpersonation();
                context.StartImpersonation(usr2.Id);

                var role = Role.FromIdentifier(context, RoleTep.STARTER);

                community.SetUserRole(usr1, role);
                roles = Role.GetUserRolesForDomain(context, usr1.Id, community.Id);

                //user part of community
                Assert.AreEqual(1, roles.Length);
                Assert.AreEqual(RoleTep.STARTER, roles [0].Name);

                community.RemoveUser(usr1);
            } catch (Exception e) {
                Assert.Fail(e.Message);
            } finally {
                context.EndImpersonation();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Post the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Post(CommunityAddUserRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            try {
                context.Open();
                if (string.IsNullOrEmpty(request.Identifier))
                {
                    throw new Exception("Invalid request - missing community identifier");
                }

                User user = string.IsNullOrEmpty(request.Username) ? User.FromId(context, context.UserId) : User.FromUsername(context, request.Username);

                //we use administrator access level to be able to load the community
                context.AccessLevel = EntityAccessLevel.Administrator;
                ThematicCommunity domain = ThematicCommunity.FromIdentifier(context, request.Identifier);

                Role role = Role.FromIdentifier(context, string.IsNullOrEmpty(request.Role) ? domain.DefaultRoleName : request.Role);
                context.LogInfo(this, string.Format("/community/user POST Identifier='{0}', Username='******', Role='{2}'", request.Identifier, user.Username, role.Identifier));

                if (string.IsNullOrEmpty(request.Username))
                {
                    //case user auto Join
                    domain.TryJoinCurrentUser(request.Objectives);
                }
                else
                {
                    //case owner add user with role
                    domain.SetUserRole(user, role);
                }

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new WebResponseBool(true));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Put the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Put(CommunityUpdateUserRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            try {
                context.Open();
                if (string.IsNullOrEmpty(request.Identifier))
                {
                    throw new Exception("Invalid request - missing community identifier");
                }
                if (string.IsNullOrEmpty(request.Username))
                {
                    throw new Exception("Invalid request - missing username");
                }
                if (string.IsNullOrEmpty(request.Role))
                {
                    throw new Exception("Invalid request - missing role");
                }

                User user = User.FromUsername(context, request.Username);
                Role role = Role.FromIdentifier(context, request.Role);
                context.LogInfo(this, string.Format("/community/user PUT Identifier='{0}', Username='******', Role='{2}'", request.Identifier, user.Username, role.Identifier));

                ThematicCommunity domain = ThematicCommunity.FromIdentifier(context, request.Identifier);

                domain.SetUserRole(user, role);

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new WebResponseBool(true));
        }