/// <summary> /// Post the specified request. /// </summary> /// <param name="request">Request.</param> public object Post(UserCreateRequestTep request) { var context = TepWebContext.GetWebContext(PagePrivileges.UserView); WebUserTep result; try{ context.Open(); UserTep user = (request.Id == 0 ? null : UserTep.FromId(context, request.Id)); user = request.ToEntity(context, user); if (request.Id != 0 && context.UserLevel == UserLevel.Administrator) { user.AccountStatus = AccountStatusType.Enabled; } else { user.AccountStatus = AccountStatusType.PendingActivation; } user.IsNormalAccount = true; user.Level = UserLevel.User; user.Store(); context.LogInfo(this, string.Format("/user POST Id='{0}'", user.Id)); context.LogDebug(this, string.Format("User '{0}' has been created", user.Username)); result = new WebUserTep(context, user); context.Close(); }catch (Exception e) { context.LogError(this, e.Message, e); context.Close(); throw e; } return(result); }
/// <summary> /// Update the specified user. /// </summary> /// <param name="request">Request.</param> /// <returns>the user</returns> public object Put(UserUpdateRequestTep request) { var context = TepWebContext.GetWebContext(PagePrivileges.UserView); WebUserTep result; try { context.Open(); context.LogInfo(this, string.Format("/user PUT Id='{0}'", request.Id > 0 ? request.Id + "" : request.Identifier)); UserTep user = (request.Id == 0 ? (!string.IsNullOrEmpty(request.Identifier) ? UserTep.FromIdentifier(context, request.Identifier) : null) : UserTep.FromId(context, request.Id)); if (context.UserId != user.Id && context.AccessLevel != EntityAccessLevel.Administrator) { throw new Exception("Action not allowed"); } var level = user.Level; user = request.ToEntity(context, user); user.Level = level;//we can only change the level from the dedicated request (admin only) user.Store(); context.LogDebug(this, string.Format("User '{0}' has been updated", user.Username)); result = new WebUserTep(context, user); context.Close(); } catch (Exception e) { context.LogError(this, e.Message, e); context.Close(); throw e; } return(result); }
private void Init() { //Create users UserTep usr1 = new UserTep(context); usr1.Username = "******"; usr1.Store(); UserTep usr2 = new UserTep(context); usr2.Username = "******"; usr2.Store(); UserTep usr3 = new UserTep(context); usr3.Username = "******"; usr3.Store(); UserTep usr4 = new UserTep(context); usr4.Username = "******"; usr4.Store(); //create domains Domain domain = new Domain(context); domain.Identifier = "myDomainTest"; domain.Kind = DomainKind.Public; domain.Store(); Domain domain2 = new Domain(context); domain2.Identifier = "otherDomainTest"; domain2.Kind = DomainKind.Hidden; domain2.Store(); Role role = new Role(context); role.Identifier = "member-test"; role.Store(); role.IncludePrivilege(Privilege.FromIdentifier(context, "wpsjob-v")); role.IncludePrivilege(Privilege.FromIdentifier(context, "wpsjob-s")); //Add users in the domain role.GrantToUser(usr1, domain); role.GrantToUser(usr2, domain); role.GrantToUser(usr3, domain); role.GrantToUser(usr3, domain2); //create community ThematicCommunity community1 = new ThematicCommunity(context); community1.Identifier = "community-public-1"; community1.Kind = DomainKind.Public; community1.Store(); community1.SetOwner(usr3); }
private void Init() { //Create users UserTep usr1 = new UserTep(context); usr1.Username = "******"; usr1.Store(); UserTep usr2 = new UserTep(context); usr2.Username = "******"; usr2.Store(); UserTep usr3 = new UserTep(context); usr3.Username = "******"; usr3.Store(); //create communities ThematicCommunity community1 = new ThematicCommunity(context); community1.Identifier = "community-public-1"; community1.Kind = DomainKind.Public; community1.Store(); community1.SetOwner(usr2); ThematicCommunity community2 = new ThematicCommunity(context); community2.Identifier = "community-private-1"; community2.Kind = DomainKind.Hidden; community2.Store(); community2.SetOwner(usr2); ThematicCommunity community3 = new ThematicCommunity(context); community3.Identifier = "community-private-2"; community3.Kind = DomainKind.Hidden; community3.Store(); community3.SetOwner(usr2); ThematicCommunity community4 = new ThematicCommunity(context); community4.Identifier = "community-public-2"; community4.Kind = DomainKind.Public; community4.Store(); community4.SetOwner(usr2); }
public object Put(UserUpdateAdminRequestTep request) { var context = TepWebContext.GetWebContext(PagePrivileges.AdminOnly); WebUserTep result; try { context.Open(); context.LogInfo(this, string.Format("/user/admin PUT Id='{0}'", request.Id)); UserTep user = (request.Id == 0 ? null : UserTep.FromId(context, request.Id)); user.Level = request.Level; user.Store(); context.LogDebug(this, string.Format("Level of user '{0}' has been updated to Level {1}", user.Username, request.Level)); result = new WebUserTep(context, user); context.Close(); } catch (Exception e) { context.LogError(this, e.Message, e); context.Close(); throw e; } return(result); }
public object Delete(UserDeleteApiKeyRequestTep request) { var context = TepWebContext.GetWebContext(PagePrivileges.UserView); WebUserTep result; try { context.Open(); context.LogInfo(this, string.Format("/user/key DELETE Id='{0}'", context.UserId)); UserTep user = UserTep.FromId(context, context.UserId); user.ApiKey = null; user.Store(); result = new WebUserTep(context, user); context.Close(); } catch (Exception e) { context.LogError(this, e.Message, e); context.Close(); throw e; } return(result); }
public object Put(UserUpdateStatusRequestTep request) { var context = TepWebContext.GetWebContext(PagePrivileges.AdminOnly); WebUserTep result; try { context.Open(); context.LogInfo(this, string.Format("/user/status PUT Id='{0}',Status='{1}'", request.Id > 0 ? request.Id + "" : request.Identifier, request.AccountStatus)); UserTep user = (request.Id == 0 ? (!string.IsNullOrEmpty(request.Identifier) ? UserTep.FromIdentifier(context, request.Identifier) : null) : UserTep.FromId(context, request.Id)); user.AccountStatus = request.AccountStatus; user.Store(); context.LogDebug(this, string.Format("Status of user '{0}' has been updated to {1}", user.Username, request.AccountStatus)); result = new WebUserTep(context, user); context.Close(); } catch (Exception e) { context.LogError(this, e.Message, e); context.Close(); throw e; } return(result); }
public override User GetUserProfile(IfyWebContext context, HttpRequest request = null, bool strict = false) { NewUserCreated = false; UserTep usr = null; AuthenticationType authType = IfyWebContext.GetAuthenticationType(typeof(TepLdapAuthenticationType)); var tokenrefresh = DBCookie.LoadDBCookie(context, context.GetConfigValue("cookieID-token-refresh")); var tokenaccess = DBCookie.LoadDBCookie(context, context.GetConfigValue("cookieID-token-access")); context.LogDebug(this, string.Format("GetUserProfile -- tokenrefresh = {0} ; tokenaccess = {1}", tokenrefresh.Value, tokenaccess.Value)); if (!string.IsNullOrEmpty(tokenrefresh.Value) && DateTime.UtcNow > tokenaccess.Expire) { // refresh the token try { var tokenresponse = client.RefreshToken(tokenrefresh.Value); DBCookie.StoreDBCookie(context, context.GetConfigValue("cookieID-token-access"), tokenresponse.access_token, tokenaccess.Username, tokenresponse.expires_in); DBCookie.StoreDBCookie(context, context.GetConfigValue("cookieID-token-refresh"), tokenresponse.refresh_token, tokenrefresh.Username); DBCookie.StoreDBCookie(context, context.GetConfigValue("cookieID-token-id"), tokenresponse.id_token, tokenrefresh.Username, tokenresponse.expires_in); tokenaccess = DBCookie.LoadDBCookie(context, context.GetConfigValue("cookieID-token-access")); context.LogDebug(this, string.Format("GetUserProfile - refresh -- tokenrefresh = {0} ; tokenaccess = {1}", tokenrefresh.Value, tokenaccess.Value)); } catch (Exception) { return(null); } } if (!string.IsNullOrEmpty(tokenaccess.Value)) { OauthUserInfoResponse usrInfo = client.GetUserInfo(tokenaccess.Value); context.LogDebug(this, string.Format("GetUserProfile -- usrInfo")); if (usrInfo == null) { return(null); } context.LogDebug(this, string.Format("GetUserProfile -- usrInfo = {0}", usrInfo.sub)); //Check if association auth / username exists int userId = User.GetUserId(context, usrInfo.sub, authType); bool userHasAuthAssociated = userId != 0; //user has ldap auth associated to his account if (userHasAuthAssociated) { //User exists, we load it usr = UserTep.FromId(context, userId); //test if TerradueCloudUsername was set if (string.IsNullOrEmpty(usr.TerradueCloudUsername)) { usr.LoadCloudUsername(); if (string.IsNullOrEmpty(usr.TerradueCloudUsername)) { usr.TerradueCloudUsername = usrInfo.sub; usr.StoreCloudUsername(); } } //update user infos if (!string.IsNullOrEmpty(usrInfo.given_name)) { usr.FirstName = usrInfo.given_name; } if (!string.IsNullOrEmpty(usrInfo.family_name)) { usr.LastName = usrInfo.family_name; } if (!string.IsNullOrEmpty(usrInfo.zoneinfo)) { usr.TimeZone = usrInfo.zoneinfo; } if (!string.IsNullOrEmpty(usrInfo.locale)) { usr.Language = usrInfo.locale; } return(usr); } if (string.IsNullOrEmpty(usrInfo.email)) { throw new Exception("Null email returned by the Oauth mechanism, please contact support."); } //user does not have ldap auth associated to his account try { //check if a user with the same email exists usr = UserTep.FromEmail(context, usrInfo.email); //user with the same email exists but not yet associated to ldap auth usr.LinkToAuthenticationProvider(authType, usrInfo.sub); return(usr); //TODO: what about if user Cloud username is different ? force to new one ? } catch (Exception e) { context.LogError(this, e.Message); } //user with this email does not exist, we should create it usr = (UserTep)User.GetOrCreate(context, usrInfo.sub, authType); usr.Level = UserCreationDefaultLevel; //update user infos if (!string.IsNullOrEmpty(usrInfo.given_name)) { usr.FirstName = usrInfo.given_name; } if (!string.IsNullOrEmpty(usrInfo.family_name)) { usr.LastName = usrInfo.family_name; } if (!string.IsNullOrEmpty(usrInfo.email) && (TrustEmail || usrInfo.email_verifier)) { usr.Email = usrInfo.email; } if (!string.IsNullOrEmpty(usrInfo.zoneinfo)) { usr.TimeZone = usrInfo.zoneinfo; } if (!string.IsNullOrEmpty(usrInfo.locale)) { usr.Language = usrInfo.locale; } if (usr.Id == 0) { usr.AccessLevel = EntityAccessLevel.Administrator; NewUserCreated = true; } usr.Store(); usr.LinkToAuthenticationProvider(authType, usrInfo.sub); usr.TerradueCloudUsername = usrInfo.sub; usr.StoreCloudUsername(); return(usr); } else { } context.LogDebug(this, string.Format("GetUserProfile -- return null")); return(null); }