Esempio n. 1
0
        public ActionResult <string> Register([FromBody] RegisterRequestModel registerRequestModel)
        {
            if (_databaseService.Users.FirstOrDefault(t => t.Username == registerRequestModel.Username) != null)
            {
                throw new UsernameDuplicatedException("Username duplicated.");
            }

            // 创建用户
            User user = new User();

            user.Username = registerRequestModel.Username;
            user.Password = BCrypt.Net.BCrypt.HashPassword(registerRequestModel.Password);
            user.Nickname = registerRequestModel.Nickname;
            user.Status   = 0;
            _databaseService.Users.Add(user);
            _databaseService.SaveChanges();

            // 创建用户组信息
            GroupToUser groupToUser = new GroupToUser()
            {
                GroupId = Group.GroupID.DEFAULT,
                UserId  = user.Id
            };

            _databaseService.GroupsToUsersRelation.Add(groupToUser);

            // 创建用户目录
            var groupDirectory = EntityFile.CreateDirectoryRecord(user.Username, "/users", $"/users/{user.Username}", user);

            _databaseService.Files.Add(groupDirectory);

            _databaseService.SaveChanges();

            return(Ok(new RegisterResultModel(user)));
        }
        public IActionResult AddGroupMember([FromBody] GroupAddMemberRequestModel addGroupMemberRequestModel, string groupname)
        {
            if (!Regex.IsMatch(groupname, @"^[a-zA-Z0-9-_]{4,16}$"))
            {
                throw new GroupnameInvalidException("The groupname you enter is invalid when trying to add a member to it.");
            }
            var group = _databaseService.Groups.FirstOrDefault(t => t.GroupName == groupname);

            if (group == null)
            {
                throw new GroupNotExistException("The groupname you enter does not exsit actually when trying to add a grouptouser.");
            }
            string permission = PermissionBank.GroupOperationPermission(groupname, "member", "add");
            var    user_actor = HttpContext.Items["actor"] as User;

            if (user_actor.HasPermission(permission) != true)
            {
                throw new AuthenticateFailedException("not has enough permission when trying to add a member to a group.");
            }
            var user = _databaseService.Users.FirstOrDefault(t => t.Username == addGroupMemberRequestModel.UserName);

            if (user == null)
            {
                throw new UserNotExistException("The username you enter does not exist actually  when trying to add a grouptouser");
            }
            var grouptouser = _databaseService.GroupsToUsersRelation.FirstOrDefault(t => t.Group.GroupName == group.GroupName && t.User.Username == user.Username);

            if (grouptouser != null)
            {
                throw new GroupToUserAlreadyExistException("Grouptouser already exists when trying to add a grouptouser");
            }

            grouptouser         = new GroupToUser();
            grouptouser.Group   = group;
            grouptouser.GroupId = group.Id;
            grouptouser.User    = user;
            grouptouser.UserId  = user.Id;
            _databaseService.GroupsToUsersRelation.Add(grouptouser);
            _databaseService.SaveChanges();

            return(Ok(new GroupMemberAddResultModel(group, user)));
        }
Esempio n. 3
0
        public IActionResult AddGroup([FromBody] GroupCreateRequestModel addGroupRequestModel)
        {
            //use groupname to identify group,because the id is invisible to user
            if (_databaseService.Groups.FirstOrDefault(t => t.GroupName == addGroupRequestModel.GroupName) != null)
            {
                throw new GroupnameDuplicatedException("Groupname duplicated.");
            }
            //initialize new group and save it to database
            Group group = new Group();

            group.GroupName = addGroupRequestModel.GroupName;
            _databaseService.Groups.Add(group);
            //obtain the user
            var user = HttpContext.Items["actor"] as User;

            //initialize grouptouser and save it to database
            GroupToUser groupToUser = new GroupToUser();

            groupToUser.Group   = group;
            groupToUser.GroupId = group.Id;
            groupToUser.User    = user;
            groupToUser.UserId  = user.Id;
            _databaseService.GroupsToUsersRelation.Add(groupToUser);

            // initial group permission to the new group
            _databaseService.UserToPermissionRelation.Add(new UserToPermission()
            {
                User       = user,
                UserId     = user.Id,
                Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "add")
            });

            _databaseService.UserToPermissionRelation.Add(new UserToPermission()
            {
                User       = user,
                UserId     = user.Id,
                Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "remove")
            });

            _databaseService.UserToPermissionRelation.Add(new UserToPermission()
            {
                User       = user,
                UserId     = user.Id,
                Permission = PermissionBank.GroupOperationPermission(group.GroupName, "", "delete")
            });

            _databaseService.GroupsToPermissionsRelation.Add(new GroupToPermission()
            {
                Group      = group,
                GroupId    = group.Id,
                Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "list")
            });

            //find the grouptouser in the database
            //below is how to input parameters when the entity has composite key values:
            //"The ordering of composite key values is as defined in the EDM, which is in turn as defined in the designer, by the Code First fluent API, or by the DataMember attribute."
            // var groupToUser_db = _databaseService.GroupsToUsersRelation.Find(groupToUser.GroupId, groupToUser.UserId);

            //waiting for adding permissions for the group

            if (!_databaseService.Files.Any(s => s.Path == $"/groups/{group.GroupName}"))
            {
                var groupDirectory = EntityFile.CreateDirectoryRecord(group.GroupName, "/groups", $"/groups/{group.GroupName}", user);
                _databaseService.Files.Add(groupDirectory);
            }
            _databaseService.SaveChanges();

            return(Ok(new GroupCreateResultModel(group)));
        }