James Moger
2014-05-29 a3456e22504a0a054312989fd52a21e4d193baf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2014 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.wicket.panels;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
 
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.GitBlitWebSession;
 
/**
 * A radio group panel of the 5 available authorization/access restriction combinations.
 *
 * @author James Moger
 *
 */
public class RepositoryNamePanel extends BasePanel {
 
    private static final long serialVersionUID = 1L;
 
    private final RepositoryModel repository;
 
    private String fullName;
 
    public RepositoryNamePanel(String wicketId, RepositoryModel repository) {
        super(wicketId);
        this.repository = repository;
    }
 
    @Override
    protected void onInitialize() {
        super.onInitialize();
 
        GitBlitWebSession session = GitBlitWebSession.get();
        UserModel user = session.getUser();
 
        // build project list for repository destination
        String defaultProject = null;
        List<String> projects = new ArrayList<String>();
 
        if (user.canAdmin()) {
            projects.add("/");
            defaultProject = "/";
        }
 
        if (user.canCreate()) {
            String p =  user.getPersonalPath() + "/";
            projects.add(p);
            if (defaultProject == null) {
                // only prefer personal namespace if default is not already set
                defaultProject = p;
            }
        }
 
        repository.projectPath = defaultProject;
 
        add(new DropDownChoice<String>("projectPath", projects));
        add(new TextField<String>("name"));
    }
 
    public boolean updateModel(RepositoryModel repositoryModel) {
        // confirm a project was selected
        if (StringUtils.isEmpty(repositoryModel.projectPath)) {
            error(getString("gb.pleaseSelectProject"));
            return false;
        }
 
        // confirm a repository name was entered
        if (StringUtils.isEmpty(repositoryModel.name)) {
            error(getString("gb.pleaseSetRepositoryName"));
            return false;
        }
 
        String project = repositoryModel.projectPath;
 
        fullName = (project + repositoryModel.name).trim();
        fullName = fullName.replace('\\', '/');
        fullName = fullName.replace("//", "/");
        if (fullName.charAt(0) == '/') {
            fullName = fullName.substring(1);
        }
        if (fullName.endsWith("/")) {
            fullName = fullName.substring(0, fullName.length() - 1);
        }
 
        if (fullName.contains("../")) {
            error(getString("gb.illegalRelativeSlash"));
            return false;
        }
        if (fullName.contains("/../")) {
            error(getString("gb.illegalRelativeSlash"));
            return false;
        }
 
        // confirm valid characters in repository name
        Character c = StringUtils.findInvalidCharacter(fullName);
        if (c != null) {
            error(MessageFormat.format(getString("gb.illegalCharacterRepositoryName"), c));
            return false;
        }
 
        repositoryModel.name = fullName;
        repositoryModel.projectPath = null;
 
        return true;
    }
 
    public void resetModel(RepositoryModel repositoryModel) {
        // restore project and name fields on error condition
        repositoryModel.projectPath = StringUtils.getFirstPathElement(fullName) + "/";
        if (repositoryModel.projectPath.length() > 1) {
            repositoryModel.name = fullName.substring(repositoryModel.projectPath.length());
        }
    }
 
    @Override
    protected boolean getStatelessHint() {
        return false;
    }
}