Exemple #1
0
 public ProtocolMember(ProtocolUser protocolUser)
 {
     Email             = protocolUser.User.Email;
     FirstName         = protocolUser.User.FirstName;
     LastName          = protocolUser.User.LastName;
     ProtocolPrivilege = protocolUser.Privilege;
 }
        public async Task <object> CreateProtocol([FromBody] CreateProtocol data)
        {
            Guid     procotolid;
            Protocol protocol = null;

            //check if this protocol has already been uploaded
            if (Guid.TryParse(data.Id, out procotolid))
            {
                protocol = user.ProtocolUsers.Select(x => x.Protocol).FirstOrDefault(p => p.Id.Equals(procotolid));
                protocol = user.GroupUsers.Select(x => x.Group).SelectMany(g => g.GroupProtocols)
                           .Select(x => x.Protocol).FirstOrDefault(p => p.Id.Equals(procotolid));
            }

            //if not present in user visible range then create a new one
            if (protocol == null)
            {
                //create the protocol
                protocol = new Protocol
                {
                    Id                = Guid.NewGuid(),
                    Name              = data.Name,
                    Isprivate         = data.Isprivate,
                    Comment           = data.Comment,
                    IStudy            = data.Istudy ?? 0,
                    UseDefaultAnat    = data.Usedefaultanat ?? true,
                    UseDefaultChannel = data.Usedefaultchannel ?? true,
                    LastUpdate        = System.DateTime.Now
                };
                //give user admin priviledge
                var protocoluser = new ProtocolUser
                {
                    Id       = Guid.NewGuid(),
                    User     = user,
                    Protocol = protocol,
                    //become protocol admin by default
                    Privilege = 1
                };

                //save changes to database
                context.Protocols.Add(protocol);
                context.ProtocolUsers.Add(protocoluser);
                await context.SaveChangesAsync();
            }

            //lock the protocol since creation is a write action
            session.Protocolid = protocol.Id;
            return(new ID
            {
                Id = protocol.Id
            });
        }
        public async Task <object> AddOrEditUser([FromBody] EditUserProtocolRelationIn data)
        {
            //check if user is protocol admin

            var userProtocolRelation = user.ProtocolUsers.FirstOrDefault(x => x.Protocol.Id.Equals(data.Protocolid));

            if (userProtocolRelation == null || userProtocolRelation.Privilege > 1)
            {
                Unauthorized("You are not protocol admin.");
            }
            //find target user protocol relation
            var targetUserProtocolRelation = context.ProtocolUsers
                                             .FirstOrDefault(x => x.Protocol.Id.Equals(data.Protocolid) && x.User.Email.Equals(data.Useremail));
            var privildge = data.Privilege;

            if (privildge < 1 || privildge > 3)
            {
                privildge = 3;
            }
            if (targetUserProtocolRelation == null)
            {
                var targetuser = context.Users.FirstOrDefault(u => u.Email.Equals(data.Useremail));
                if (targetuser == null)
                {
                    return(NotFound("The user you want to edit doesn't exist."));
                }

                //create relation
                var newTargetUserProtocolRelation = new ProtocolUser
                {
                    Id        = Guid.NewGuid(),
                    User      = targetuser,
                    Protocol  = userProtocolRelation.Protocol,
                    Privilege = privildge
                };
                context.ProtocolUsers.Add(newTargetUserProtocolRelation);
                await context.SaveChangesAsync();

                return(newTargetUserProtocolRelation.Id);
            }
            else
            {
                //edit relation
                targetUserProtocolRelation.Privilege = privildge;
                await context.SaveChangesAsync();

                return(targetUserProtocolRelation.Id);
            }
        }