Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/29/11 17:16:42 (13 years ago)
Author:
ascheibe
Message:

#1648 worked on webservice

Location:
branches/ClientUserManagement/HeuristicLab.Services.Access/3.3
Files:
1 added
4 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/AccessService.cs

    r6840 r6852  
    2424using System.Linq;
    2525using System.ServiceModel;
     26using System.Web.Security;
    2627using DA = HeuristicLab.Services.Access.DataAccess;
    2728using DT = HeuristicLab.Services.Access.DataTransfer;
     
    3031  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    3132  public class AccessService : IAccessService {
     33    private IUserManager userManager;
     34    private IUserManager UserManager {
     35      get {
     36        if (userManager == null) userManager = new UserManager();
     37        return userManager;
     38      }
     39    }
    3240
    3341    #region Client Members
     
    8391    public void DeleteClient(DT.Client client) {
    8492      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
    85         context.Resources.DeleteOnSubmit(Convert.ToEntity(client));
    86         context.SubmitChanges();
     93        //load client because we could get a detached object
     94        var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
     95                    where c.Id == client.Id
     96                    select c;
     97        if (query.Count() > 0) {
     98          context.Resources.DeleteOnSubmit(query.First());
     99          context.SubmitChanges();
     100        }
    87101      }
    88102    }
     
    120134    }
    121135
    122     public void DeleteClientGroup(DT.ClientGroup group) {
    123       using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
    124         context.Resources.DeleteOnSubmit(Convert.ToEntity(group));
    125         context.SubmitChanges();
    126       }
    127     }
    128 
    129     public void AddResourceToGroup(DT.Resource resource, DT.UserGroup group) {
     136    public void DeleteClientGroup(DT.ClientGroup clientGroup) {
     137      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     138        //load clientGroup because we could get a detached object
     139        var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
     140                    where c.Id == clientGroup.Id
     141                    select c;
     142        if (query.Count() > 0) {
     143          context.Resources.DeleteOnSubmit(query.First());
     144          context.SubmitChanges();
     145        }
     146      }
     147    }
     148
     149    public void AddResourceToGroup(DT.Resource resource, DT.ClientGroup group) {
    130150      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
    131151        DA.ResourceResourceGroup rrg = new DA.ResourceResourceGroup() {
     
    139159    }
    140160
    141     public void RemoveResourceFromGroup(DT.Resource resource, DT.UserGroup group) {
     161    public void RemoveResourceFromGroup(DT.Resource resource, DT.ClientGroup group) {
    142162      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
    143163        var query = context.ResourceResourceGroups.Where(x => x.ResourceId == resource.Id && x.ResourceGroupId == group.Id);
     
    214234
    215235    #region User
     236    private DT.User BuildUserDto(Guid userId) {
     237      DA.aspnet_User aspUser = null;
     238      DA.aspnet_Membership aspMembership = null;
     239      DA.User accessUser = null;
     240
     241      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     242        var query = from u in context.UserGroupBases.OfType<DA.User>()
     243                    where u.Id == userId
     244                    select u;
     245        if (query.Count() == 1) {
     246          accessUser = query.First();
     247        }
     248      }
     249
     250      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
     251        var userQuery = from u in context.aspnet_Users
     252                        where u.UserId == userId
     253                        select u;
     254        if (userQuery.Count() == 1) {
     255          aspUser = userQuery.First();
     256        }
     257
     258        var memQuery = from u in context.aspnet_Memberships
     259                       where u.UserId == userId
     260                       select u;
     261        if (memQuery.Count() == 1) {
     262          aspMembership = memQuery.First();
     263        }
     264      }
     265
     266      if (aspUser == null || aspMembership == null || accessUser == null) {
     267        //TODO: error handling
     268        return null;
     269      } else {
     270        return Convert.ToDto(accessUser, aspUser, aspMembership);
     271      }
     272    }
     273
    216274    public DT.User Login() {
    217       throw new System.NotImplementedException();
     275      Guid userId = UserManager.CurrentUserId;
     276      return BuildUserDto(userId);
    218277    }
    219278
    220279    public IEnumerable<DT.User> GetAllUsers() {
    221       throw new System.NotImplementedException();
    222     }
    223 
    224     public IEnumerable<DT.User> GetUsers(IEnumerable<DT.User> ids) {
    225       throw new System.NotImplementedException();
     280      List<Guid> accessUserGuids = null;
     281
     282      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     283        var query = from u in context.UserGroupBases.OfType<DA.User>()
     284                    select u.Id;
     285        accessUserGuids = query.ToList();
     286      }
     287
     288      return accessUserGuids.Select(x => BuildUserDto(x));
     289    }
     290
     291    public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
     292      List<Guid> accessUserGuids = null;
     293
     294      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     295        var query = from u in context.UserGroupBases.OfType<DA.User>()
     296                    where ids.Contains(u.Id)
     297                    select u.Id;
     298        accessUserGuids = query.ToList();
     299      }
     300
     301      return accessUserGuids.Select(x => BuildUserDto(x));
    226302    }
    227303
    228304    public DT.User AddUser(DT.User user) {
    229       throw new System.NotImplementedException();
     305      DA.User accessUser;
     306      DA.aspnet_User aspUser;
     307      DA.aspnet_Membership aspMembership;
     308      bool userExistsInASP;
     309
     310      Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
     311
     312      if (userExistsInASP) {
     313        if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
     314          accessUser.Id = aspMembership.UserId;
     315        }
     316        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     317          context.UserGroupBases.InsertOnSubmit(accessUser);
     318          context.SubmitChanges();
     319        }
     320        MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
     321        if (membershipUser != null) {
     322          membershipUser.Email = aspMembership.Email;
     323          membershipUser.IsApproved = aspMembership.IsApproved;
     324          membershipUser.Comment = aspMembership.Comment;
     325          Membership.UpdateUser(membershipUser);
     326        }
     327      } else {
     328        MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
     329        membershipUser.IsApproved = aspMembership.IsApproved;
     330        membershipUser.Comment = aspMembership.Comment;
     331        Membership.UpdateUser(membershipUser);
     332
     333        Guid userId = (Guid)membershipUser.ProviderUserKey;
     334        accessUser.Id = userId;
     335
     336        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     337          context.UserGroupBases.InsertOnSubmit(accessUser);
     338          context.SubmitChanges();
     339        }
     340      }
     341
     342      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
     343        var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
     344        var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
     345        return Convert.ToDto(accessUser, newAspUser, newAspMembership);
     346      }
    230347    }
    231348
    232349    public void DeleteUser(DT.User user) {
    233       throw new System.NotImplementedException();
     350      if (user.Id != null && user.Id != Guid.Empty) {
     351        Membership.DeleteUser(user.UserName);
     352        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     353          context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(user));
     354          context.SubmitChanges();
     355        }
     356      }
    234357    }
    235358
    236359    public void UpdateUser(DT.User user) {
    237       throw new System.NotImplementedException();
     360      AddUser(user);
    238361    }
    239362
    240363    public void AddUserToRole(DT.Role role, DT.User user) {
    241       throw new System.NotImplementedException();
     364      //TODO: usernames and rolenames have to be unique!
     365      MembershipUser msUser = Membership.GetUser((object)user.Id);
     366      if (msUser != null) {
     367        Roles.AddUserToRole(msUser.UserName, role.Name);
     368      }
    242369    }
    243370
    244371    public void RemoveUserFromRole(DT.Role role, DT.User user) {
    245       throw new System.NotImplementedException();
    246     }
    247 
    248     public void ResetPassword(DT.User user, string password) {
    249       throw new System.NotImplementedException();
    250     }
    251     #endregion
    252 
     372      MembershipUser msUser = Membership.GetUser((object)user.Id);
     373      if (msUser != null) {
     374        Roles.RemoveUserFromRole(msUser.UserName, role.Name);
     375      }
     376    }
     377
     378    public bool ResetPassword(DT.User user, string oldPassword, string newPassword) {
     379      MembershipUser msUser = Membership.GetUser((object)user.Id);
     380      if (msUser != null) {
     381        return msUser.ChangePassword(oldPassword, newPassword);
     382      }
     383      return false;
     384    }
     385    #endregion
     386
     387    #region UserGroup
    253388    public IEnumerable<DT.UserGroup> GetAllUserGroups() {
    254       throw new System.NotImplementedException();
     389      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     390        var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
     391                    select Convert.ToDto(u);
     392        return query.ToList();
     393      }
    255394    }
    256395
    257396    public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
    258       throw new System.NotImplementedException();
    259     }
    260 
    261     public DT.UserGroup AddUserGroup(DT.UserGroup group) {
    262       throw new System.NotImplementedException();
     397      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     398        var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
     399                    where ids.Contains(u.Id)
     400                    select Convert.ToDto(u);
     401        return query.ToList();
     402      }
     403    }
     404
     405    public Guid AddUserGroup(DT.UserGroup group) {
     406      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     407        context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
     408        context.SubmitChanges();
     409        return group.Id;
     410      }
    263411    }
    264412
    265413    public void UpdateUserGroup(DT.UserGroup group) {
    266       throw new System.NotImplementedException();
     414      AddUserGroup(group);
    267415    }
    268416
    269417    public void DeleteUserGroup(DT.UserGroup group) {
    270       throw new System.NotImplementedException();
     418      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     419        context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(group));
     420        context.SubmitChanges();
     421      }
    271422    }
    272423
    273424    public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
    274       throw new System.NotImplementedException();
    275     }
    276 
    277     public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup group) {
    278       throw new System.NotImplementedException();
     425      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     426        DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
     427        ugug.UserGroupId = resource.Id;
     428        ugug.UserGroupUserGroupId = group.Id;
     429        context.UserGroupUserGroups.InsertOnSubmit(ugug);
     430        context.SubmitChanges();
     431      }
     432    }
     433
     434    public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
     435      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     436        var query = from u in context.UserGroupUserGroups
     437                    where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
     438                    select u;
     439
     440        if (query.Count() == 1) {
     441          context.UserGroupUserGroups.DeleteOnSubmit(query.First());
     442          context.SubmitChanges();
     443        }
     444      }
    279445    }
    280446
    281447    public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
    282       throw new System.NotImplementedException();
     448      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     449        var query = from u in context.UserGroupBases
     450                    select Convert.ToDto(u);
     451        return query.ToList();
     452      }
    283453    }
    284454
    285455    public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
    286       throw new System.NotImplementedException();
    287     }
    288 
     456      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
     457        var query = from u in context.UserGroupUserGroups
     458                    select Convert.ToDto(u);
     459        return query.ToList();
     460      }
     461    }
     462    #endregion
     463
     464    #region Roles
    289465    public IEnumerable<DT.Role> GetRoles() {
    290466      throw new System.NotImplementedException();
     
    306482      throw new System.NotImplementedException();
    307483    }
    308 
     484    #endregion
     485
     486    #region Error Reporting
    309487    public void ReportError(DT.ClientError error) {
    310488      throw new System.NotImplementedException();
     
    322500      throw new System.NotImplementedException();
    323501    }
     502    #endregion
    324503  }
    325504}
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/Convert.cs

    r6840 r6852  
    6868    #region Country
    6969    public static DT.Country ToDto(DA.Country source) {
    70       return new DT.Country() {
    71         Id = source.Id,
    72         Name = source.Name
    73       };
     70      if (source == null) {
     71        return null;
     72      } else {
     73        return new DT.Country() {
     74          Id = source.Id,
     75          Name = source.Name
     76        };
     77      }
    7478    }
    7579
    7680    public static DA.Country ToEntity(DT.Country source) {
    77       return new DA.Country() {
    78         Id = source.Id,
    79         Name = source.Name,
    80 
    81       };
     81      if (source == null) {
     82        return null;
     83      } else {
     84        return new DA.Country() {
     85          Id = source.Id,
     86          Name = source.Name,
     87
     88        };
     89      }
    8290    }
    8391    #endregion
     
    8593    #region OperatingSystem
    8694    public static DT.OperatingSystem ToDto(DA.OperatingSystem source) {
    87       return new DT.OperatingSystem() {
    88         Id = source.Id,
    89         Name = source.Name
    90       };
     95      if (source == null) {
     96        return null;
     97      } else {
     98        return new DT.OperatingSystem() {
     99          Id = source.Id,
     100          Name = source.Name
     101        };
     102      }
    91103    }
    92104
    93105    public static DA.OperatingSystem ToEntity(DT.OperatingSystem source) {
    94       return new DA.OperatingSystem() {
    95         Id = source.Id,
    96         Name = source.Name,
    97       };
     106      if (source == null) {
     107        return null;
     108      } else {
     109        return new DA.OperatingSystem() {
     110          Id = source.Id,
     111          Name = source.Name,
     112        };
     113      }
    98114    }
    99115    #endregion
     
    101117    #region ClientType
    102118    public static DT.ClientType ToDto(DA.ClientType source) {
    103       return new DT.ClientType() {
    104         Id = source.Id,
    105         Name = source.Name
    106       };
     119      if (source == null) {
     120        return null;
     121      } else {
     122        return new DT.ClientType() {
     123          Id = source.Id,
     124          Name = source.Name
     125        };
     126      }
    107127    }
    108128
    109129    public static DA.ClientType ToEntity(DT.ClientType source) {
    110       return new DA.ClientType() {
    111         Id = source.Id,
    112         Name = source.Name,
    113 
    114       };
     130      if (source == null) {
     131        return null;
     132      } else {
     133        return new DA.ClientType() {
     134          Id = source.Id,
     135          Name = source.Name,
     136
     137        };
     138      }
    115139    }
    116140    #endregion
     
    118142    #region ClientConfiguration
    119143    public static DT.ClientConfiguration ToDto(DA.ClientConfiguration source) {
    120       return new DT.ClientConfiguration() {
    121         Id = source.Id,
    122         Hash = source.Hash,
    123         Description = source.Description
    124       };
     144      if (source == null) {
     145        return null;
     146      } else {
     147        return new DT.ClientConfiguration() {
     148          Id = source.Id,
     149          Hash = source.Hash,
     150          Description = source.Description
     151        };
     152      }
    125153    }
    126154
    127155    public static DA.ClientConfiguration ToEntity(DT.ClientConfiguration source) {
    128       return new DA.ClientConfiguration() {
    129         Id = source.Id,
    130         Hash = source.Hash,
    131         Description = source.Description
    132       };
     156      if (source == null) {
     157        return null;
     158      } else {
     159        return new DA.ClientConfiguration() {
     160          Id = source.Id,
     161          Hash = source.Hash,
     162          Description = source.Description
     163        };
     164      }
    133165    }
    134166    #endregion
     
    264296        UserName = aspUserSource.UserName
    265297      };
     298    }
     299
     300    public static DA.User ToEntity(DT.User source) {
     301      return new DA.User() { Id = source.Id, FullName = source.FullName };
    266302    }
    267303
     
    302338
    303339    #region ClientGroupMapping
    304     public static DT.ClientGroupMapping ToDto(DA.ResourceResourceGroup c) {
     340    public static DT.ClientGroupMapping ToDto(DA.ResourceResourceGroup source) {
    305341      return new DT.ClientGroupMapping() {
    306         Child = c.ResourceId, Parent = c.ResourceGroupId
    307       };
    308     }
    309     #endregion
    310 
    311 
     342        Child = source.ResourceId, Parent = source.ResourceGroupId
     343      };
     344    }
     345    #endregion
     346
     347    #region UserGroupBase
     348    public static DT.UserGroupBase ToDto(DA.UserGroupBase source) {
     349      return new DT.UserGroupBase() {
     350        Id = source.Id
     351      };
     352    }
     353    #endregion
     354
     355    #region UserGroupMapping
     356    public static DT.UserGroupMapping ToDto(DA.UserGroupUserGroup source) {
     357      return new DT.UserGroupMapping() {
     358        Child = source.UserGroupId, Parent = source.UserGroupUserGroupId
     359      };
     360    }
     361    #endregion
    312362  }
    313363}
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/HeuristicLab.Services.Access-3.3.csproj

    r6840 r6852  
    4848    <Reference Include="System.Runtime.Serialization" />
    4949    <Reference Include="System.ServiceModel" />
     50    <Reference Include="System.Web" />
     51    <Reference Include="System.Web.ApplicationServices" />
    5052    <Reference Include="System.Xml" />
    5153    <Reference Include="System.Xml.Linq" />
     
    7173    <Compile Include="DataTransfer\UserGroupMapping.cs" />
    7274    <Compile Include="IAccessService.cs" />
     75    <Compile Include="Interfaces\IUserManager.cs" />
    7376    <Compile Include="Properties\AssemblyInfo.cs" />
    7477    <Compile Include="AccessService.cs" />
     78    <Compile Include="UserManager.cs" />
    7579  </ItemGroup>
    7680  <ItemGroup>
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/IAccessService.cs

    r6840 r6852  
    7070
    7171    [OperationContract]
    72     void AddResourceToGroup(Resource resource, UserGroup group);
     72    void AddResourceToGroup(Resource resource, ClientGroup group);
    7373
    7474    [OperationContract]
    75     void RemoveResourceFromGroup(Resource resource, UserGroup group);
     75    void RemoveResourceFromGroup(Resource resource, ClientGroup group);
    7676    #endregion
    7777
     
    111111
    112112    [OperationContract]
    113     IEnumerable<User> GetUsers(IEnumerable<User> ids);
     113    IEnumerable<User> GetUsers(IEnumerable<Guid> ids);
    114114
    115115    [OperationContract]
     
    129129
    130130    [OperationContract]
    131     void ResetPassword(User user, string password);
     131    bool ResetPassword(User user, string oldPassword, string newPassword);
    132132    #endregion
    133133
     
    140140
    141141    [OperationContract]
    142     UserGroup AddUserGroup(UserGroup group);
     142    Guid AddUserGroup(UserGroup group);
    143143
    144144    [OperationContract]
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/Interfaces/IUserManager.cs

    r6822 r6852  
    2323using System.Web.Security;
    2424
    25 namespace HeuristicLab.Services.Hive {
     25namespace HeuristicLab.Services.Access {
    2626  public interface IUserManager {
    2727    MembershipUser CurrentUser { get; }
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/UserManager.cs

    r6822 r6852  
    2323using System.Web.Security;
    2424
    25 namespace HeuristicLab.Services.Hive {
     25namespace HeuristicLab.Services.Access {
    2626  public class UserManager : IUserManager {
    2727    public MembershipUser CurrentUser {
Note: See TracChangeset for help on using the changeset viewer.